mmc.c 29 KB

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