mmc.c 30 KB

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