mmc.c 24 KB

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