mmc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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. card->host->host_name, 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. card->host->host_name, 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. cmd.opcode = MMC_GO_IDLE_STATE;
  379. cmd.arg = 0;
  380. cmd.flags = MMC_RSP_NONE;
  381. mmc_wait_for_cmd(host, &cmd, 0);
  382. mmc_delay(1);
  383. }
  384. /*
  385. * Apply power to the MMC stack.
  386. */
  387. static void mmc_power_up(struct mmc_host *host)
  388. {
  389. int bit = fls(host->ocr_avail) - 1;
  390. host->ios.vdd = bit;
  391. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  392. host->ios.power_mode = MMC_POWER_UP;
  393. host->ops->set_ios(host, &host->ios);
  394. mmc_delay(1);
  395. host->ios.clock = host->f_min;
  396. host->ios.power_mode = MMC_POWER_ON;
  397. host->ops->set_ios(host, &host->ios);
  398. mmc_delay(2);
  399. }
  400. static void mmc_power_off(struct mmc_host *host)
  401. {
  402. host->ios.clock = 0;
  403. host->ios.vdd = 0;
  404. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  405. host->ios.power_mode = MMC_POWER_OFF;
  406. host->ops->set_ios(host, &host->ios);
  407. }
  408. static int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
  409. {
  410. struct mmc_command cmd;
  411. int i, err = 0;
  412. cmd.opcode = MMC_SEND_OP_COND;
  413. cmd.arg = ocr;
  414. cmd.flags = MMC_RSP_R3;
  415. for (i = 100; i; i--) {
  416. err = mmc_wait_for_cmd(host, &cmd, 0);
  417. if (err != MMC_ERR_NONE)
  418. break;
  419. if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
  420. break;
  421. err = MMC_ERR_TIMEOUT;
  422. mmc_delay(10);
  423. }
  424. if (rocr)
  425. *rocr = cmd.resp[0];
  426. return err;
  427. }
  428. /*
  429. * Discover cards by requesting their CID. If this command
  430. * times out, it is not an error; there are no further cards
  431. * to be discovered. Add new cards to the list.
  432. *
  433. * Create a mmc_card entry for each discovered card, assigning
  434. * it an RCA, and save the raw CID for decoding later.
  435. */
  436. static void mmc_discover_cards(struct mmc_host *host)
  437. {
  438. struct mmc_card *card;
  439. unsigned int first_rca = 1, err;
  440. while (1) {
  441. struct mmc_command cmd;
  442. cmd.opcode = MMC_ALL_SEND_CID;
  443. cmd.arg = 0;
  444. cmd.flags = MMC_RSP_R2;
  445. err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
  446. if (err == MMC_ERR_TIMEOUT) {
  447. err = MMC_ERR_NONE;
  448. break;
  449. }
  450. if (err != MMC_ERR_NONE) {
  451. printk(KERN_ERR "%s: error requesting CID: %d\n",
  452. host->host_name, err);
  453. break;
  454. }
  455. card = mmc_find_card(host, cmd.resp);
  456. if (!card) {
  457. card = mmc_alloc_card(host, cmd.resp, &first_rca);
  458. if (IS_ERR(card)) {
  459. err = PTR_ERR(card);
  460. break;
  461. }
  462. list_add(&card->node, &host->cards);
  463. }
  464. card->state &= ~MMC_STATE_DEAD;
  465. cmd.opcode = MMC_SET_RELATIVE_ADDR;
  466. cmd.arg = card->rca << 16;
  467. cmd.flags = MMC_RSP_R1;
  468. err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
  469. if (err != MMC_ERR_NONE)
  470. mmc_card_set_dead(card);
  471. }
  472. }
  473. static void mmc_read_csds(struct mmc_host *host)
  474. {
  475. struct mmc_card *card;
  476. list_for_each_entry(card, &host->cards, node) {
  477. struct mmc_command cmd;
  478. int err;
  479. if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
  480. continue;
  481. cmd.opcode = MMC_SEND_CSD;
  482. cmd.arg = card->rca << 16;
  483. cmd.flags = MMC_RSP_R2;
  484. err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
  485. if (err != MMC_ERR_NONE) {
  486. mmc_card_set_dead(card);
  487. continue;
  488. }
  489. memcpy(card->raw_csd, cmd.resp, sizeof(card->raw_csd));
  490. mmc_decode_csd(card);
  491. mmc_decode_cid(card);
  492. }
  493. }
  494. static unsigned int mmc_calculate_clock(struct mmc_host *host)
  495. {
  496. struct mmc_card *card;
  497. unsigned int max_dtr = host->f_max;
  498. list_for_each_entry(card, &host->cards, node)
  499. if (!mmc_card_dead(card) && max_dtr > card->csd.max_dtr)
  500. max_dtr = card->csd.max_dtr;
  501. DBG("MMC: selected %d.%03dMHz transfer rate\n",
  502. max_dtr / 1000000, (max_dtr / 1000) % 1000);
  503. return max_dtr;
  504. }
  505. /*
  506. * Check whether cards we already know about are still present.
  507. * We do this by requesting status, and checking whether a card
  508. * responds.
  509. *
  510. * A request for status does not cause a state change in data
  511. * transfer mode.
  512. */
  513. static void mmc_check_cards(struct mmc_host *host)
  514. {
  515. struct list_head *l, *n;
  516. mmc_deselect_cards(host);
  517. list_for_each_safe(l, n, &host->cards) {
  518. struct mmc_card *card = mmc_list_to_card(l);
  519. struct mmc_command cmd;
  520. int err;
  521. cmd.opcode = MMC_SEND_STATUS;
  522. cmd.arg = card->rca << 16;
  523. cmd.flags = MMC_RSP_R1;
  524. err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
  525. if (err == MMC_ERR_NONE)
  526. continue;
  527. mmc_card_set_dead(card);
  528. }
  529. }
  530. static void mmc_setup(struct mmc_host *host)
  531. {
  532. if (host->ios.power_mode != MMC_POWER_ON) {
  533. int err;
  534. u32 ocr;
  535. mmc_power_up(host);
  536. mmc_idle_cards(host);
  537. err = mmc_send_op_cond(host, 0, &ocr);
  538. if (err != MMC_ERR_NONE)
  539. return;
  540. host->ocr = mmc_select_voltage(host, ocr);
  541. /*
  542. * Since we're changing the OCR value, we seem to
  543. * need to tell some cards to go back to the idle
  544. * state. We wait 1ms to give cards time to
  545. * respond.
  546. */
  547. if (host->ocr)
  548. mmc_idle_cards(host);
  549. } else {
  550. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  551. host->ios.clock = host->f_min;
  552. host->ops->set_ios(host, &host->ios);
  553. /*
  554. * We should remember the OCR mask from the existing
  555. * cards, and detect the new cards OCR mask, combine
  556. * the two and re-select the VDD. However, if we do
  557. * change VDD, we should do an idle, and then do a
  558. * full re-initialisation. We would need to notify
  559. * drivers so that they can re-setup the cards as
  560. * well, while keeping their queues at bay.
  561. *
  562. * For the moment, we take the easy way out - if the
  563. * new cards don't like our currently selected VDD,
  564. * they drop off the bus.
  565. */
  566. }
  567. if (host->ocr == 0)
  568. return;
  569. /*
  570. * Send the selected OCR multiple times... until the cards
  571. * all get the idea that they should be ready for CMD2.
  572. * (My SanDisk card seems to need this.)
  573. */
  574. mmc_send_op_cond(host, host->ocr, NULL);
  575. mmc_discover_cards(host);
  576. /*
  577. * Ok, now switch to push-pull mode.
  578. */
  579. host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
  580. host->ops->set_ios(host, &host->ios);
  581. mmc_read_csds(host);
  582. }
  583. /**
  584. * mmc_detect_change - process change of state on a MMC socket
  585. * @host: host which changed state.
  586. *
  587. * All we know is that card(s) have been inserted or removed
  588. * from the socket(s). We don't know which socket or cards.
  589. */
  590. void mmc_detect_change(struct mmc_host *host)
  591. {
  592. schedule_work(&host->detect);
  593. }
  594. EXPORT_SYMBOL(mmc_detect_change);
  595. static void mmc_rescan(void *data)
  596. {
  597. struct mmc_host *host = data;
  598. struct list_head *l, *n;
  599. mmc_claim_host(host);
  600. if (host->ios.power_mode == MMC_POWER_ON)
  601. mmc_check_cards(host);
  602. mmc_setup(host);
  603. if (!list_empty(&host->cards)) {
  604. /*
  605. * (Re-)calculate the fastest clock rate which the
  606. * attached cards and the host support.
  607. */
  608. host->ios.clock = mmc_calculate_clock(host);
  609. host->ops->set_ios(host, &host->ios);
  610. }
  611. mmc_release_host(host);
  612. list_for_each_safe(l, n, &host->cards) {
  613. struct mmc_card *card = mmc_list_to_card(l);
  614. /*
  615. * If this is a new and good card, register it.
  616. */
  617. if (!mmc_card_present(card) && !mmc_card_dead(card)) {
  618. if (mmc_register_card(card))
  619. mmc_card_set_dead(card);
  620. else
  621. mmc_card_set_present(card);
  622. }
  623. /*
  624. * If this card is dead, destroy it.
  625. */
  626. if (mmc_card_dead(card)) {
  627. list_del(&card->node);
  628. mmc_remove_card(card);
  629. }
  630. }
  631. /*
  632. * If we discover that there are no cards on the
  633. * bus, turn off the clock and power down.
  634. */
  635. if (list_empty(&host->cards))
  636. mmc_power_off(host);
  637. }
  638. /**
  639. * mmc_alloc_host - initialise the per-host structure.
  640. * @extra: sizeof private data structure
  641. * @dev: pointer to host device model structure
  642. *
  643. * Initialise the per-host structure.
  644. */
  645. struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
  646. {
  647. struct mmc_host *host;
  648. host = kmalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
  649. if (host) {
  650. memset(host, 0, sizeof(struct mmc_host) + extra);
  651. spin_lock_init(&host->lock);
  652. init_waitqueue_head(&host->wq);
  653. INIT_LIST_HEAD(&host->cards);
  654. INIT_WORK(&host->detect, mmc_rescan, host);
  655. host->dev = dev;
  656. /*
  657. * By default, hosts do not support SGIO or large requests.
  658. * They have to set these according to their abilities.
  659. */
  660. host->max_hw_segs = 1;
  661. host->max_phys_segs = 1;
  662. host->max_sectors = 1 << (PAGE_CACHE_SHIFT - 9);
  663. host->max_seg_size = PAGE_CACHE_SIZE;
  664. }
  665. return host;
  666. }
  667. EXPORT_SYMBOL(mmc_alloc_host);
  668. /**
  669. * mmc_add_host - initialise host hardware
  670. * @host: mmc host
  671. */
  672. int mmc_add_host(struct mmc_host *host)
  673. {
  674. static unsigned int host_num;
  675. snprintf(host->host_name, sizeof(host->host_name),
  676. "mmc%d", host_num++);
  677. mmc_power_off(host);
  678. mmc_detect_change(host);
  679. return 0;
  680. }
  681. EXPORT_SYMBOL(mmc_add_host);
  682. /**
  683. * mmc_remove_host - remove host hardware
  684. * @host: mmc host
  685. *
  686. * Unregister and remove all cards associated with this host,
  687. * and power down the MMC bus.
  688. */
  689. void mmc_remove_host(struct mmc_host *host)
  690. {
  691. struct list_head *l, *n;
  692. list_for_each_safe(l, n, &host->cards) {
  693. struct mmc_card *card = mmc_list_to_card(l);
  694. mmc_remove_card(card);
  695. }
  696. mmc_power_off(host);
  697. }
  698. EXPORT_SYMBOL(mmc_remove_host);
  699. /**
  700. * mmc_free_host - free the host structure
  701. * @host: mmc host
  702. *
  703. * Free the host once all references to it have been dropped.
  704. */
  705. void mmc_free_host(struct mmc_host *host)
  706. {
  707. flush_scheduled_work();
  708. kfree(host);
  709. }
  710. EXPORT_SYMBOL(mmc_free_host);
  711. #ifdef CONFIG_PM
  712. /**
  713. * mmc_suspend_host - suspend a host
  714. * @host: mmc host
  715. * @state: suspend mode (PM_SUSPEND_xxx)
  716. */
  717. int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
  718. {
  719. mmc_claim_host(host);
  720. mmc_deselect_cards(host);
  721. mmc_power_off(host);
  722. mmc_release_host(host);
  723. return 0;
  724. }
  725. EXPORT_SYMBOL(mmc_suspend_host);
  726. /**
  727. * mmc_resume_host - resume a previously suspended host
  728. * @host: mmc host
  729. */
  730. int mmc_resume_host(struct mmc_host *host)
  731. {
  732. mmc_detect_change(host);
  733. return 0;
  734. }
  735. EXPORT_SYMBOL(mmc_resume_host);
  736. #endif
  737. MODULE_LICENSE("GPL");