mmc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  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 <linux/mmc/card.h>
  20. #include <linux/mmc/host.h>
  21. #include <linux/mmc/protocol.h>
  22. #include "mmc.h"
  23. #ifdef CONFIG_MMC_DEBUG
  24. #define DBG(x...) printk(KERN_DEBUG x)
  25. #else
  26. #define DBG(x...) do { } while (0)
  27. #endif
  28. #define CMD_RETRIES 3
  29. /*
  30. * OCR Bit positions to 10s of Vdd mV.
  31. */
  32. static const unsigned short mmc_ocr_bit_to_vdd[] = {
  33. 150, 155, 160, 165, 170, 180, 190, 200,
  34. 210, 220, 230, 240, 250, 260, 270, 280,
  35. 290, 300, 310, 320, 330, 340, 350, 360
  36. };
  37. static const unsigned int tran_exp[] = {
  38. 10000, 100000, 1000000, 10000000,
  39. 0, 0, 0, 0
  40. };
  41. static const unsigned char tran_mant[] = {
  42. 0, 10, 12, 13, 15, 20, 25, 30,
  43. 35, 40, 45, 50, 55, 60, 70, 80,
  44. };
  45. static const unsigned int tacc_exp[] = {
  46. 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
  47. };
  48. static const unsigned int tacc_mant[] = {
  49. 0, 10, 12, 13, 15, 20, 25, 30,
  50. 35, 40, 45, 50, 55, 60, 70, 80,
  51. };
  52. /**
  53. * mmc_request_done - finish processing an MMC command
  54. * @host: MMC host which completed command
  55. * @mrq: MMC request which completed
  56. *
  57. * MMC drivers should call this function when they have completed
  58. * their processing of a command. This should be called before the
  59. * data part of the command has completed.
  60. */
  61. void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
  62. {
  63. struct mmc_command *cmd = mrq->cmd;
  64. int err = mrq->cmd->error;
  65. DBG("MMC: req done (%02x): %d: %08x %08x %08x %08x\n", cmd->opcode,
  66. err, 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. DBG("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_claim_host - exclusively claim a host
  144. * @host: mmc host to claim
  145. * @card: mmc card to claim host for
  146. *
  147. * Claim a host for a set of operations. If a valid card
  148. * is passed and this wasn't the last card selected, select
  149. * the card before returning.
  150. *
  151. * Note: you should use mmc_card_claim_host or mmc_claim_host.
  152. */
  153. int __mmc_claim_host(struct mmc_host *host, struct mmc_card *card)
  154. {
  155. DECLARE_WAITQUEUE(wait, current);
  156. unsigned long flags;
  157. int err = 0;
  158. add_wait_queue(&host->wq, &wait);
  159. spin_lock_irqsave(&host->lock, flags);
  160. while (1) {
  161. set_current_state(TASK_UNINTERRUPTIBLE);
  162. if (host->card_busy == NULL)
  163. break;
  164. spin_unlock_irqrestore(&host->lock, flags);
  165. schedule();
  166. spin_lock_irqsave(&host->lock, flags);
  167. }
  168. set_current_state(TASK_RUNNING);
  169. host->card_busy = card;
  170. spin_unlock_irqrestore(&host->lock, flags);
  171. remove_wait_queue(&host->wq, &wait);
  172. if (card != (void *)-1 && host->card_selected != card) {
  173. struct mmc_command cmd;
  174. host->card_selected = card;
  175. cmd.opcode = MMC_SELECT_CARD;
  176. cmd.arg = card->rca << 16;
  177. cmd.flags = MMC_RSP_R1;
  178. err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
  179. }
  180. return err;
  181. }
  182. EXPORT_SYMBOL(__mmc_claim_host);
  183. /**
  184. * mmc_release_host - release a host
  185. * @host: mmc host to release
  186. *
  187. * Release a MMC host, allowing others to claim the host
  188. * for their operations.
  189. */
  190. void mmc_release_host(struct mmc_host *host)
  191. {
  192. unsigned long flags;
  193. BUG_ON(host->card_busy == NULL);
  194. spin_lock_irqsave(&host->lock, flags);
  195. host->card_busy = NULL;
  196. spin_unlock_irqrestore(&host->lock, flags);
  197. wake_up(&host->wq);
  198. }
  199. EXPORT_SYMBOL(mmc_release_host);
  200. /*
  201. * Ensure that no card is selected.
  202. */
  203. static void mmc_deselect_cards(struct mmc_host *host)
  204. {
  205. struct mmc_command cmd;
  206. if (host->card_selected) {
  207. host->card_selected = NULL;
  208. cmd.opcode = MMC_SELECT_CARD;
  209. cmd.arg = 0;
  210. cmd.flags = MMC_RSP_NONE;
  211. mmc_wait_for_cmd(host, &cmd, 0);
  212. }
  213. }
  214. static inline void mmc_delay(unsigned int ms)
  215. {
  216. if (ms < HZ / 1000) {
  217. yield();
  218. mdelay(ms);
  219. } else {
  220. msleep_interruptible (ms);
  221. }
  222. }
  223. /*
  224. * Mask off any voltages we don't support and select
  225. * the lowest voltage
  226. */
  227. static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
  228. {
  229. int bit;
  230. ocr &= host->ocr_avail;
  231. bit = ffs(ocr);
  232. if (bit) {
  233. bit -= 1;
  234. ocr = 3 << bit;
  235. host->ios.vdd = bit;
  236. host->ops->set_ios(host, &host->ios);
  237. } else {
  238. ocr = 0;
  239. }
  240. return ocr;
  241. }
  242. #define UNSTUFF_BITS(resp,start,size) \
  243. ({ \
  244. const int __size = size; \
  245. const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
  246. const int __off = 3 - ((start) / 32); \
  247. const int __shft = (start) & 31; \
  248. u32 __res; \
  249. \
  250. __res = resp[__off] >> __shft; \
  251. if (__size + __shft > 32) \
  252. __res |= resp[__off-1] << ((32 - __shft) % 32); \
  253. __res & __mask; \
  254. })
  255. /*
  256. * Given the decoded CSD structure, decode the raw CID to our CID structure.
  257. */
  258. static void mmc_decode_cid(struct mmc_card *card)
  259. {
  260. u32 *resp = card->raw_cid;
  261. memset(&card->cid, 0, sizeof(struct mmc_cid));
  262. /*
  263. * The selection of the format here is guesswork based upon
  264. * information people have sent to date.
  265. */
  266. switch (card->csd.mmca_vsn) {
  267. case 0: /* MMC v1.? */
  268. case 1: /* MMC v1.4 */
  269. card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
  270. card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  271. card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  272. card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  273. card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  274. card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  275. card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
  276. card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
  277. card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
  278. card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
  279. card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
  280. card->cid.month = UNSTUFF_BITS(resp, 12, 4);
  281. card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
  282. break;
  283. case 2: /* MMC v2.x ? */
  284. case 3: /* MMC v3.x ? */
  285. card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
  286. card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
  287. card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  288. card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  289. card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  290. card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  291. card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  292. card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
  293. card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
  294. card->cid.month = UNSTUFF_BITS(resp, 12, 4);
  295. card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
  296. break;
  297. default:
  298. printk("%s: card has unknown MMCA version %d\n",
  299. mmc_hostname(card->host), card->csd.mmca_vsn);
  300. mmc_card_set_bad(card);
  301. break;
  302. }
  303. }
  304. /*
  305. * Given a 128-bit response, decode to our card CSD structure.
  306. */
  307. static void mmc_decode_csd(struct mmc_card *card)
  308. {
  309. struct mmc_csd *csd = &card->csd;
  310. unsigned int e, m, csd_struct;
  311. u32 *resp = card->raw_csd;
  312. /*
  313. * We only understand CSD structure v1.1 and v2.
  314. * v2 has extra information in bits 15, 11 and 10.
  315. */
  316. csd_struct = UNSTUFF_BITS(resp, 126, 2);
  317. if (csd_struct != 1 && csd_struct != 2) {
  318. printk("%s: unrecognised CSD structure version %d\n",
  319. mmc_hostname(card->host), csd_struct);
  320. mmc_card_set_bad(card);
  321. return;
  322. }
  323. csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
  324. m = UNSTUFF_BITS(resp, 115, 4);
  325. e = UNSTUFF_BITS(resp, 112, 3);
  326. csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
  327. csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
  328. m = UNSTUFF_BITS(resp, 99, 4);
  329. e = UNSTUFF_BITS(resp, 96, 3);
  330. csd->max_dtr = tran_exp[e] * tran_mant[m];
  331. csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
  332. e = UNSTUFF_BITS(resp, 47, 3);
  333. m = UNSTUFF_BITS(resp, 62, 12);
  334. csd->capacity = (1 + m) << (e + 2);
  335. csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
  336. }
  337. /*
  338. * Locate a MMC card on this MMC host given a raw CID.
  339. */
  340. static struct mmc_card *mmc_find_card(struct mmc_host *host, u32 *raw_cid)
  341. {
  342. struct mmc_card *card;
  343. list_for_each_entry(card, &host->cards, node) {
  344. if (memcmp(card->raw_cid, raw_cid, sizeof(card->raw_cid)) == 0)
  345. return card;
  346. }
  347. return NULL;
  348. }
  349. /*
  350. * Allocate a new MMC card, and assign a unique RCA.
  351. */
  352. static struct mmc_card *
  353. mmc_alloc_card(struct mmc_host *host, u32 *raw_cid, unsigned int *frca)
  354. {
  355. struct mmc_card *card, *c;
  356. unsigned int rca = *frca;
  357. card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL);
  358. if (!card)
  359. return ERR_PTR(-ENOMEM);
  360. mmc_init_card(card, host);
  361. memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid));
  362. again:
  363. list_for_each_entry(c, &host->cards, node)
  364. if (c->rca == rca) {
  365. rca++;
  366. goto again;
  367. }
  368. card->rca = rca;
  369. *frca = rca;
  370. return card;
  371. }
  372. /*
  373. * Tell attached cards to go to IDLE state
  374. */
  375. static void mmc_idle_cards(struct mmc_host *host)
  376. {
  377. struct mmc_command cmd;
  378. host->ios.chip_select = MMC_CS_HIGH;
  379. host->ops->set_ios(host, &host->ios);
  380. mmc_delay(1);
  381. cmd.opcode = MMC_GO_IDLE_STATE;
  382. cmd.arg = 0;
  383. cmd.flags = MMC_RSP_NONE;
  384. mmc_wait_for_cmd(host, &cmd, 0);
  385. mmc_delay(1);
  386. host->ios.chip_select = MMC_CS_DONTCARE;
  387. host->ops->set_ios(host, &host->ios);
  388. mmc_delay(1);
  389. }
  390. /*
  391. * Apply power to the MMC stack.
  392. */
  393. static void mmc_power_up(struct mmc_host *host)
  394. {
  395. int bit = fls(host->ocr_avail) - 1;
  396. host->ios.vdd = bit;
  397. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  398. host->ios.chip_select = MMC_CS_DONTCARE;
  399. host->ios.power_mode = MMC_POWER_UP;
  400. host->ops->set_ios(host, &host->ios);
  401. mmc_delay(1);
  402. host->ios.clock = host->f_min;
  403. host->ios.power_mode = MMC_POWER_ON;
  404. host->ops->set_ios(host, &host->ios);
  405. mmc_delay(2);
  406. }
  407. static void mmc_power_off(struct mmc_host *host)
  408. {
  409. host->ios.clock = 0;
  410. host->ios.vdd = 0;
  411. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  412. host->ios.chip_select = MMC_CS_DONTCARE;
  413. host->ios.power_mode = MMC_POWER_OFF;
  414. host->ops->set_ios(host, &host->ios);
  415. }
  416. static int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
  417. {
  418. struct mmc_command cmd;
  419. int i, err = 0;
  420. cmd.opcode = MMC_SEND_OP_COND;
  421. cmd.arg = ocr;
  422. cmd.flags = MMC_RSP_R3;
  423. for (i = 100; i; i--) {
  424. err = mmc_wait_for_cmd(host, &cmd, 0);
  425. if (err != MMC_ERR_NONE)
  426. break;
  427. if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
  428. break;
  429. err = MMC_ERR_TIMEOUT;
  430. mmc_delay(10);
  431. }
  432. if (rocr)
  433. *rocr = cmd.resp[0];
  434. return err;
  435. }
  436. /*
  437. * Discover cards by requesting their CID. If this command
  438. * times out, it is not an error; there are no further cards
  439. * to be discovered. Add new cards to the list.
  440. *
  441. * Create a mmc_card entry for each discovered card, assigning
  442. * it an RCA, and save the raw CID for decoding later.
  443. */
  444. static void mmc_discover_cards(struct mmc_host *host)
  445. {
  446. struct mmc_card *card;
  447. unsigned int first_rca = 1, err;
  448. while (1) {
  449. struct mmc_command cmd;
  450. cmd.opcode = MMC_ALL_SEND_CID;
  451. cmd.arg = 0;
  452. cmd.flags = MMC_RSP_R2;
  453. err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
  454. if (err == MMC_ERR_TIMEOUT) {
  455. err = MMC_ERR_NONE;
  456. break;
  457. }
  458. if (err != MMC_ERR_NONE) {
  459. printk(KERN_ERR "%s: error requesting CID: %d\n",
  460. mmc_hostname(host), err);
  461. break;
  462. }
  463. card = mmc_find_card(host, cmd.resp);
  464. if (!card) {
  465. card = mmc_alloc_card(host, cmd.resp, &first_rca);
  466. if (IS_ERR(card)) {
  467. err = PTR_ERR(card);
  468. break;
  469. }
  470. list_add(&card->node, &host->cards);
  471. }
  472. card->state &= ~MMC_STATE_DEAD;
  473. cmd.opcode = MMC_SET_RELATIVE_ADDR;
  474. cmd.arg = card->rca << 16;
  475. cmd.flags = MMC_RSP_R1;
  476. err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
  477. if (err != MMC_ERR_NONE)
  478. mmc_card_set_dead(card);
  479. }
  480. }
  481. static void mmc_read_csds(struct mmc_host *host)
  482. {
  483. struct mmc_card *card;
  484. list_for_each_entry(card, &host->cards, node) {
  485. struct mmc_command cmd;
  486. int err;
  487. if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
  488. continue;
  489. cmd.opcode = MMC_SEND_CSD;
  490. cmd.arg = card->rca << 16;
  491. cmd.flags = MMC_RSP_R2;
  492. err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
  493. if (err != MMC_ERR_NONE) {
  494. mmc_card_set_dead(card);
  495. continue;
  496. }
  497. memcpy(card->raw_csd, cmd.resp, sizeof(card->raw_csd));
  498. mmc_decode_csd(card);
  499. mmc_decode_cid(card);
  500. }
  501. }
  502. static unsigned int mmc_calculate_clock(struct mmc_host *host)
  503. {
  504. struct mmc_card *card;
  505. unsigned int max_dtr = host->f_max;
  506. list_for_each_entry(card, &host->cards, node)
  507. if (!mmc_card_dead(card) && max_dtr > card->csd.max_dtr)
  508. max_dtr = card->csd.max_dtr;
  509. DBG("MMC: selected %d.%03dMHz transfer rate\n",
  510. max_dtr / 1000000, (max_dtr / 1000) % 1000);
  511. return max_dtr;
  512. }
  513. /*
  514. * Check whether cards we already know about are still present.
  515. * We do this by requesting status, and checking whether a card
  516. * responds.
  517. *
  518. * A request for status does not cause a state change in data
  519. * transfer mode.
  520. */
  521. static void mmc_check_cards(struct mmc_host *host)
  522. {
  523. struct list_head *l, *n;
  524. mmc_deselect_cards(host);
  525. list_for_each_safe(l, n, &host->cards) {
  526. struct mmc_card *card = mmc_list_to_card(l);
  527. struct mmc_command cmd;
  528. int err;
  529. cmd.opcode = MMC_SEND_STATUS;
  530. cmd.arg = card->rca << 16;
  531. cmd.flags = MMC_RSP_R1;
  532. err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
  533. if (err == MMC_ERR_NONE)
  534. continue;
  535. mmc_card_set_dead(card);
  536. }
  537. }
  538. static void mmc_setup(struct mmc_host *host)
  539. {
  540. if (host->ios.power_mode != MMC_POWER_ON) {
  541. int err;
  542. u32 ocr;
  543. mmc_power_up(host);
  544. mmc_idle_cards(host);
  545. err = mmc_send_op_cond(host, 0, &ocr);
  546. if (err != MMC_ERR_NONE)
  547. return;
  548. host->ocr = mmc_select_voltage(host, ocr);
  549. /*
  550. * Since we're changing the OCR value, we seem to
  551. * need to tell some cards to go back to the idle
  552. * state. We wait 1ms to give cards time to
  553. * respond.
  554. */
  555. if (host->ocr)
  556. mmc_idle_cards(host);
  557. } else {
  558. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  559. host->ios.clock = host->f_min;
  560. host->ops->set_ios(host, &host->ios);
  561. /*
  562. * We should remember the OCR mask from the existing
  563. * cards, and detect the new cards OCR mask, combine
  564. * the two and re-select the VDD. However, if we do
  565. * change VDD, we should do an idle, and then do a
  566. * full re-initialisation. We would need to notify
  567. * drivers so that they can re-setup the cards as
  568. * well, while keeping their queues at bay.
  569. *
  570. * For the moment, we take the easy way out - if the
  571. * new cards don't like our currently selected VDD,
  572. * they drop off the bus.
  573. */
  574. }
  575. if (host->ocr == 0)
  576. return;
  577. /*
  578. * Send the selected OCR multiple times... until the cards
  579. * all get the idea that they should be ready for CMD2.
  580. * (My SanDisk card seems to need this.)
  581. */
  582. mmc_send_op_cond(host, host->ocr, NULL);
  583. mmc_discover_cards(host);
  584. /*
  585. * Ok, now switch to push-pull mode.
  586. */
  587. host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
  588. host->ops->set_ios(host, &host->ios);
  589. mmc_read_csds(host);
  590. }
  591. /**
  592. * mmc_detect_change - process change of state on a MMC socket
  593. * @host: host which changed state.
  594. *
  595. * All we know is that card(s) have been inserted or removed
  596. * from the socket(s). We don't know which socket or cards.
  597. */
  598. void mmc_detect_change(struct mmc_host *host)
  599. {
  600. schedule_work(&host->detect);
  601. }
  602. EXPORT_SYMBOL(mmc_detect_change);
  603. static void mmc_rescan(void *data)
  604. {
  605. struct mmc_host *host = data;
  606. struct list_head *l, *n;
  607. mmc_claim_host(host);
  608. if (host->ios.power_mode == MMC_POWER_ON)
  609. mmc_check_cards(host);
  610. mmc_setup(host);
  611. if (!list_empty(&host->cards)) {
  612. /*
  613. * (Re-)calculate the fastest clock rate which the
  614. * attached cards and the host support.
  615. */
  616. host->ios.clock = mmc_calculate_clock(host);
  617. host->ops->set_ios(host, &host->ios);
  618. }
  619. mmc_release_host(host);
  620. list_for_each_safe(l, n, &host->cards) {
  621. struct mmc_card *card = mmc_list_to_card(l);
  622. /*
  623. * If this is a new and good card, register it.
  624. */
  625. if (!mmc_card_present(card) && !mmc_card_dead(card)) {
  626. if (mmc_register_card(card))
  627. mmc_card_set_dead(card);
  628. else
  629. mmc_card_set_present(card);
  630. }
  631. /*
  632. * If this card is dead, destroy it.
  633. */
  634. if (mmc_card_dead(card)) {
  635. list_del(&card->node);
  636. mmc_remove_card(card);
  637. }
  638. }
  639. /*
  640. * If we discover that there are no cards on the
  641. * bus, turn off the clock and power down.
  642. */
  643. if (list_empty(&host->cards))
  644. mmc_power_off(host);
  645. }
  646. /**
  647. * mmc_alloc_host - initialise the per-host structure.
  648. * @extra: sizeof private data structure
  649. * @dev: pointer to host device model structure
  650. *
  651. * Initialise the per-host structure.
  652. */
  653. struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
  654. {
  655. struct mmc_host *host;
  656. host = mmc_alloc_host_sysfs(extra, dev);
  657. if (host) {
  658. spin_lock_init(&host->lock);
  659. init_waitqueue_head(&host->wq);
  660. INIT_LIST_HEAD(&host->cards);
  661. INIT_WORK(&host->detect, mmc_rescan, host);
  662. /*
  663. * By default, hosts do not support SGIO or large requests.
  664. * They have to set these according to their abilities.
  665. */
  666. host->max_hw_segs = 1;
  667. host->max_phys_segs = 1;
  668. host->max_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
  669. host->max_seg_size = PAGE_CACHE_SIZE;
  670. }
  671. return host;
  672. }
  673. EXPORT_SYMBOL(mmc_alloc_host);
  674. /**
  675. * mmc_add_host - initialise host hardware
  676. * @host: mmc host
  677. */
  678. int mmc_add_host(struct mmc_host *host)
  679. {
  680. int ret;
  681. ret = mmc_add_host_sysfs(host);
  682. if (ret == 0) {
  683. mmc_power_off(host);
  684. mmc_detect_change(host);
  685. }
  686. return ret;
  687. }
  688. EXPORT_SYMBOL(mmc_add_host);
  689. /**
  690. * mmc_remove_host - remove host hardware
  691. * @host: mmc host
  692. *
  693. * Unregister and remove all cards associated with this host,
  694. * and power down the MMC bus.
  695. */
  696. void mmc_remove_host(struct mmc_host *host)
  697. {
  698. struct list_head *l, *n;
  699. list_for_each_safe(l, n, &host->cards) {
  700. struct mmc_card *card = mmc_list_to_card(l);
  701. mmc_remove_card(card);
  702. }
  703. mmc_power_off(host);
  704. mmc_remove_host_sysfs(host);
  705. }
  706. EXPORT_SYMBOL(mmc_remove_host);
  707. /**
  708. * mmc_free_host - free the host structure
  709. * @host: mmc host
  710. *
  711. * Free the host once all references to it have been dropped.
  712. */
  713. void mmc_free_host(struct mmc_host *host)
  714. {
  715. flush_scheduled_work();
  716. mmc_free_host_sysfs(host);
  717. }
  718. EXPORT_SYMBOL(mmc_free_host);
  719. #ifdef CONFIG_PM
  720. /**
  721. * mmc_suspend_host - suspend a host
  722. * @host: mmc host
  723. * @state: suspend mode (PM_SUSPEND_xxx)
  724. */
  725. int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
  726. {
  727. mmc_claim_host(host);
  728. mmc_deselect_cards(host);
  729. mmc_power_off(host);
  730. mmc_release_host(host);
  731. return 0;
  732. }
  733. EXPORT_SYMBOL(mmc_suspend_host);
  734. /**
  735. * mmc_resume_host - resume a previously suspended host
  736. * @host: mmc host
  737. */
  738. int mmc_resume_host(struct mmc_host *host)
  739. {
  740. mmc_detect_change(host);
  741. return 0;
  742. }
  743. EXPORT_SYMBOL(mmc_resume_host);
  744. #endif
  745. MODULE_LICENSE("GPL");