mmc.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735
  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. * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  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 request
  53. * @host: MMC host which completed request
  54. * @mrq: MMC request which request
  55. *
  56. * MMC drivers should call this function when they have completed
  57. * their processing of a request.
  58. */
  59. void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
  60. {
  61. struct mmc_command *cmd = mrq->cmd;
  62. int err = cmd->error;
  63. pr_debug("%s: req done (CMD%u): %d/%d/%d: %08x %08x %08x %08x\n",
  64. mmc_hostname(host), cmd->opcode, err,
  65. mrq->data ? mrq->data->error : 0,
  66. mrq->stop ? mrq->stop->error : 0,
  67. cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
  68. if (err && cmd->retries) {
  69. cmd->retries--;
  70. cmd->error = 0;
  71. host->ops->request(host, mrq);
  72. } else if (mrq->done) {
  73. mrq->done(mrq);
  74. }
  75. }
  76. EXPORT_SYMBOL(mmc_request_done);
  77. /**
  78. * mmc_start_request - start a command on a host
  79. * @host: MMC host to start command on
  80. * @mrq: MMC request to start
  81. *
  82. * Queue a command on the specified host. We expect the
  83. * caller to be holding the host lock with interrupts disabled.
  84. */
  85. void
  86. mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
  87. {
  88. #ifdef CONFIG_MMC_DEBUG
  89. unsigned int i, sz;
  90. #endif
  91. pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
  92. mmc_hostname(host), mrq->cmd->opcode,
  93. mrq->cmd->arg, mrq->cmd->flags);
  94. WARN_ON(!host->claimed);
  95. mrq->cmd->error = 0;
  96. mrq->cmd->mrq = mrq;
  97. if (mrq->data) {
  98. BUG_ON(mrq->data->blksz > host->max_blk_size);
  99. BUG_ON(mrq->data->blocks > host->max_blk_count);
  100. BUG_ON(mrq->data->blocks * mrq->data->blksz >
  101. host->max_req_size);
  102. #ifdef CONFIG_MMC_DEBUG
  103. sz = 0;
  104. for (i = 0;i < mrq->data->sg_len;i++)
  105. sz += mrq->data->sg[i].length;
  106. BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
  107. #endif
  108. mrq->cmd->data = mrq->data;
  109. mrq->data->error = 0;
  110. mrq->data->mrq = mrq;
  111. if (mrq->stop) {
  112. mrq->data->stop = mrq->stop;
  113. mrq->stop->error = 0;
  114. mrq->stop->mrq = mrq;
  115. }
  116. }
  117. host->ops->request(host, mrq);
  118. }
  119. EXPORT_SYMBOL(mmc_start_request);
  120. static void mmc_wait_done(struct mmc_request *mrq)
  121. {
  122. complete(mrq->done_data);
  123. }
  124. int mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
  125. {
  126. DECLARE_COMPLETION_ONSTACK(complete);
  127. mrq->done_data = &complete;
  128. mrq->done = mmc_wait_done;
  129. mmc_start_request(host, mrq);
  130. wait_for_completion(&complete);
  131. return 0;
  132. }
  133. EXPORT_SYMBOL(mmc_wait_for_req);
  134. /**
  135. * mmc_wait_for_cmd - start a command and wait for completion
  136. * @host: MMC host to start command
  137. * @cmd: MMC command to start
  138. * @retries: maximum number of retries
  139. *
  140. * Start a new MMC command for a host, and wait for the command
  141. * to complete. Return any error that occurred while the command
  142. * was executing. Do not attempt to parse the response.
  143. */
  144. int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
  145. {
  146. struct mmc_request mrq;
  147. BUG_ON(!host->claimed);
  148. memset(&mrq, 0, sizeof(struct mmc_request));
  149. memset(cmd->resp, 0, sizeof(cmd->resp));
  150. cmd->retries = retries;
  151. mrq.cmd = cmd;
  152. cmd->data = NULL;
  153. mmc_wait_for_req(host, &mrq);
  154. return cmd->error;
  155. }
  156. EXPORT_SYMBOL(mmc_wait_for_cmd);
  157. /**
  158. * mmc_wait_for_app_cmd - start an application command and wait for
  159. completion
  160. * @host: MMC host to start command
  161. * @rca: RCA to send MMC_APP_CMD to
  162. * @cmd: MMC command to start
  163. * @retries: maximum number of retries
  164. *
  165. * Sends a MMC_APP_CMD, checks the card response, sends the command
  166. * in the parameter and waits for it to complete. Return any error
  167. * that occurred while the command was executing. Do not attempt to
  168. * parse the response.
  169. */
  170. int mmc_wait_for_app_cmd(struct mmc_host *host, unsigned int rca,
  171. struct mmc_command *cmd, int retries)
  172. {
  173. struct mmc_request mrq;
  174. struct mmc_command appcmd;
  175. int i, err;
  176. BUG_ON(!host->claimed);
  177. BUG_ON(retries < 0);
  178. err = MMC_ERR_INVALID;
  179. /*
  180. * We have to resend MMC_APP_CMD for each attempt so
  181. * we cannot use the retries field in mmc_command.
  182. */
  183. for (i = 0;i <= retries;i++) {
  184. memset(&mrq, 0, sizeof(struct mmc_request));
  185. appcmd.opcode = MMC_APP_CMD;
  186. appcmd.arg = rca << 16;
  187. appcmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  188. appcmd.retries = 0;
  189. memset(appcmd.resp, 0, sizeof(appcmd.resp));
  190. appcmd.data = NULL;
  191. mrq.cmd = &appcmd;
  192. appcmd.data = NULL;
  193. mmc_wait_for_req(host, &mrq);
  194. if (appcmd.error) {
  195. err = appcmd.error;
  196. continue;
  197. }
  198. /* Check that card supported application commands */
  199. if (!(appcmd.resp[0] & R1_APP_CMD))
  200. return MMC_ERR_FAILED;
  201. memset(&mrq, 0, sizeof(struct mmc_request));
  202. memset(cmd->resp, 0, sizeof(cmd->resp));
  203. cmd->retries = 0;
  204. mrq.cmd = cmd;
  205. cmd->data = NULL;
  206. mmc_wait_for_req(host, &mrq);
  207. err = cmd->error;
  208. if (cmd->error == MMC_ERR_NONE)
  209. break;
  210. }
  211. return err;
  212. }
  213. EXPORT_SYMBOL(mmc_wait_for_app_cmd);
  214. /**
  215. * mmc_set_data_timeout - set the timeout for a data command
  216. * @data: data phase for command
  217. * @card: the MMC card associated with the data transfer
  218. * @write: flag to differentiate reads from writes
  219. */
  220. void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
  221. int write)
  222. {
  223. unsigned int mult;
  224. /*
  225. * SD cards use a 100 multiplier rather than 10
  226. */
  227. mult = mmc_card_sd(card) ? 100 : 10;
  228. /*
  229. * Scale up the multiplier (and therefore the timeout) by
  230. * the r2w factor for writes.
  231. */
  232. if (write)
  233. mult <<= card->csd.r2w_factor;
  234. data->timeout_ns = card->csd.tacc_ns * mult;
  235. data->timeout_clks = card->csd.tacc_clks * mult;
  236. /*
  237. * SD cards also have an upper limit on the timeout.
  238. */
  239. if (mmc_card_sd(card)) {
  240. unsigned int timeout_us, limit_us;
  241. timeout_us = data->timeout_ns / 1000;
  242. timeout_us += data->timeout_clks * 1000 /
  243. (card->host->ios.clock / 1000);
  244. if (write)
  245. limit_us = 250000;
  246. else
  247. limit_us = 100000;
  248. /*
  249. * SDHC cards always use these fixed values.
  250. */
  251. if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
  252. data->timeout_ns = limit_us * 1000;
  253. data->timeout_clks = 0;
  254. }
  255. }
  256. }
  257. EXPORT_SYMBOL(mmc_set_data_timeout);
  258. static int mmc_select_card(struct mmc_host *host, struct mmc_card *card);
  259. /**
  260. * __mmc_claim_host - exclusively claim a host
  261. * @host: mmc host to claim
  262. * @card: mmc card to claim host for
  263. *
  264. * Claim a host for a set of operations. If a valid card
  265. * is passed and this wasn't the last card selected, select
  266. * the card before returning.
  267. *
  268. * Note: you should use mmc_card_claim_host or mmc_claim_host.
  269. */
  270. int __mmc_claim_host(struct mmc_host *host, struct mmc_card *card)
  271. {
  272. DECLARE_WAITQUEUE(wait, current);
  273. unsigned long flags;
  274. int err = 0;
  275. add_wait_queue(&host->wq, &wait);
  276. spin_lock_irqsave(&host->lock, flags);
  277. while (1) {
  278. set_current_state(TASK_UNINTERRUPTIBLE);
  279. if (!host->claimed)
  280. break;
  281. spin_unlock_irqrestore(&host->lock, flags);
  282. schedule();
  283. spin_lock_irqsave(&host->lock, flags);
  284. }
  285. set_current_state(TASK_RUNNING);
  286. host->claimed = 1;
  287. spin_unlock_irqrestore(&host->lock, flags);
  288. remove_wait_queue(&host->wq, &wait);
  289. if (card != (void *)-1) {
  290. err = mmc_select_card(host, card);
  291. if (err != MMC_ERR_NONE)
  292. return err;
  293. }
  294. return err;
  295. }
  296. EXPORT_SYMBOL(__mmc_claim_host);
  297. /**
  298. * mmc_release_host - release a host
  299. * @host: mmc host to release
  300. *
  301. * Release a MMC host, allowing others to claim the host
  302. * for their operations.
  303. */
  304. void mmc_release_host(struct mmc_host *host)
  305. {
  306. unsigned long flags;
  307. BUG_ON(!host->claimed);
  308. spin_lock_irqsave(&host->lock, flags);
  309. host->claimed = 0;
  310. spin_unlock_irqrestore(&host->lock, flags);
  311. wake_up(&host->wq);
  312. }
  313. EXPORT_SYMBOL(mmc_release_host);
  314. static inline void mmc_set_ios(struct mmc_host *host)
  315. {
  316. struct mmc_ios *ios = &host->ios;
  317. pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
  318. "width %u timing %u\n",
  319. mmc_hostname(host), ios->clock, ios->bus_mode,
  320. ios->power_mode, ios->chip_select, ios->vdd,
  321. ios->bus_width, ios->timing);
  322. host->ops->set_ios(host, ios);
  323. }
  324. static int mmc_select_card(struct mmc_host *host, struct mmc_card *card)
  325. {
  326. int err;
  327. struct mmc_command cmd;
  328. BUG_ON(!host->claimed);
  329. if (host->card_selected == card)
  330. return MMC_ERR_NONE;
  331. host->card_selected = card;
  332. cmd.opcode = MMC_SELECT_CARD;
  333. cmd.arg = card->rca << 16;
  334. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  335. err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
  336. if (err != MMC_ERR_NONE)
  337. return err;
  338. /*
  339. * We can only change the bus width of SD cards when
  340. * they are selected so we have to put the handling
  341. * here.
  342. *
  343. * The card is in 1 bit mode by default so
  344. * we only need to change if it supports the
  345. * wider version.
  346. */
  347. if (mmc_card_sd(card) &&
  348. (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
  349. /*
  350. * Default bus width is 1 bit.
  351. */
  352. host->ios.bus_width = MMC_BUS_WIDTH_1;
  353. if (host->caps & MMC_CAP_4_BIT_DATA) {
  354. struct mmc_command cmd;
  355. cmd.opcode = SD_APP_SET_BUS_WIDTH;
  356. cmd.arg = SD_BUS_WIDTH_4;
  357. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  358. err = mmc_wait_for_app_cmd(host, card->rca, &cmd,
  359. CMD_RETRIES);
  360. if (err != MMC_ERR_NONE)
  361. return err;
  362. host->ios.bus_width = MMC_BUS_WIDTH_4;
  363. }
  364. }
  365. mmc_set_ios(host);
  366. return MMC_ERR_NONE;
  367. }
  368. /*
  369. * Ensure that no card is selected.
  370. */
  371. static void mmc_deselect_cards(struct mmc_host *host)
  372. {
  373. struct mmc_command cmd;
  374. if (host->card_selected) {
  375. host->card_selected = NULL;
  376. cmd.opcode = MMC_SELECT_CARD;
  377. cmd.arg = 0;
  378. cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
  379. mmc_wait_for_cmd(host, &cmd, 0);
  380. }
  381. }
  382. static inline void mmc_delay(unsigned int ms)
  383. {
  384. if (ms < 1000 / HZ) {
  385. cond_resched();
  386. mdelay(ms);
  387. } else {
  388. msleep(ms);
  389. }
  390. }
  391. /*
  392. * Mask off any voltages we don't support and select
  393. * the lowest voltage
  394. */
  395. static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
  396. {
  397. int bit;
  398. ocr &= host->ocr_avail;
  399. bit = ffs(ocr);
  400. if (bit) {
  401. bit -= 1;
  402. ocr &= 3 << bit;
  403. host->ios.vdd = bit;
  404. mmc_set_ios(host);
  405. } else {
  406. ocr = 0;
  407. }
  408. return ocr;
  409. }
  410. #define UNSTUFF_BITS(resp,start,size) \
  411. ({ \
  412. const int __size = size; \
  413. const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
  414. const int __off = 3 - ((start) / 32); \
  415. const int __shft = (start) & 31; \
  416. u32 __res; \
  417. \
  418. __res = resp[__off] >> __shft; \
  419. if (__size + __shft > 32) \
  420. __res |= resp[__off-1] << ((32 - __shft) % 32); \
  421. __res & __mask; \
  422. })
  423. /*
  424. * Given the decoded CSD structure, decode the raw CID to our CID structure.
  425. */
  426. static void mmc_decode_cid(struct mmc_card *card)
  427. {
  428. u32 *resp = card->raw_cid;
  429. memset(&card->cid, 0, sizeof(struct mmc_cid));
  430. if (mmc_card_sd(card)) {
  431. /*
  432. * SD doesn't currently have a version field so we will
  433. * have to assume we can parse this.
  434. */
  435. card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
  436. card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
  437. card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  438. card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  439. card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  440. card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  441. card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  442. card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
  443. card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
  444. card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
  445. card->cid.year = UNSTUFF_BITS(resp, 12, 8);
  446. card->cid.month = UNSTUFF_BITS(resp, 8, 4);
  447. card->cid.year += 2000; /* SD cards year offset */
  448. } else {
  449. /*
  450. * The selection of the format here is based upon published
  451. * specs from sandisk and from what people have reported.
  452. */
  453. switch (card->csd.mmca_vsn) {
  454. case 0: /* MMC v1.0 - v1.2 */
  455. case 1: /* MMC v1.4 */
  456. card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
  457. card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  458. card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  459. card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  460. card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  461. card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  462. card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
  463. card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
  464. card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
  465. card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
  466. card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
  467. card->cid.month = UNSTUFF_BITS(resp, 12, 4);
  468. card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
  469. break;
  470. case 2: /* MMC v2.0 - v2.2 */
  471. case 3: /* MMC v3.1 - v3.3 */
  472. case 4: /* MMC v4 */
  473. card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
  474. card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
  475. card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
  476. card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
  477. card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
  478. card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
  479. card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
  480. card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
  481. card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
  482. card->cid.month = UNSTUFF_BITS(resp, 12, 4);
  483. card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
  484. break;
  485. default:
  486. printk("%s: card has unknown MMCA version %d\n",
  487. mmc_hostname(card->host), card->csd.mmca_vsn);
  488. mmc_card_set_bad(card);
  489. break;
  490. }
  491. }
  492. }
  493. /*
  494. * Given a 128-bit response, decode to our card CSD structure.
  495. */
  496. static void mmc_decode_csd(struct mmc_card *card)
  497. {
  498. struct mmc_csd *csd = &card->csd;
  499. unsigned int e, m, csd_struct;
  500. u32 *resp = card->raw_csd;
  501. if (mmc_card_sd(card)) {
  502. csd_struct = UNSTUFF_BITS(resp, 126, 2);
  503. switch (csd_struct) {
  504. case 0:
  505. m = UNSTUFF_BITS(resp, 115, 4);
  506. e = UNSTUFF_BITS(resp, 112, 3);
  507. csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
  508. csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
  509. m = UNSTUFF_BITS(resp, 99, 4);
  510. e = UNSTUFF_BITS(resp, 96, 3);
  511. csd->max_dtr = tran_exp[e] * tran_mant[m];
  512. csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
  513. e = UNSTUFF_BITS(resp, 47, 3);
  514. m = UNSTUFF_BITS(resp, 62, 12);
  515. csd->capacity = (1 + m) << (e + 2);
  516. csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
  517. csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
  518. csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
  519. csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
  520. csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
  521. csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
  522. csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
  523. break;
  524. case 1:
  525. /*
  526. * This is a block-addressed SDHC card. Most
  527. * interesting fields are unused and have fixed
  528. * values. To avoid getting tripped by buggy cards,
  529. * we assume those fixed values ourselves.
  530. */
  531. mmc_card_set_blockaddr(card);
  532. csd->tacc_ns = 0; /* Unused */
  533. csd->tacc_clks = 0; /* Unused */
  534. m = UNSTUFF_BITS(resp, 99, 4);
  535. e = UNSTUFF_BITS(resp, 96, 3);
  536. csd->max_dtr = tran_exp[e] * tran_mant[m];
  537. csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
  538. m = UNSTUFF_BITS(resp, 48, 22);
  539. csd->capacity = (1 + m) << 10;
  540. csd->read_blkbits = 9;
  541. csd->read_partial = 0;
  542. csd->write_misalign = 0;
  543. csd->read_misalign = 0;
  544. csd->r2w_factor = 4; /* Unused */
  545. csd->write_blkbits = 9;
  546. csd->write_partial = 0;
  547. break;
  548. default:
  549. printk("%s: unrecognised CSD structure version %d\n",
  550. mmc_hostname(card->host), csd_struct);
  551. mmc_card_set_bad(card);
  552. return;
  553. }
  554. } else {
  555. /*
  556. * We only understand CSD structure v1.1 and v1.2.
  557. * v1.2 has extra information in bits 15, 11 and 10.
  558. */
  559. csd_struct = UNSTUFF_BITS(resp, 126, 2);
  560. if (csd_struct != 1 && csd_struct != 2) {
  561. printk("%s: unrecognised CSD structure version %d\n",
  562. mmc_hostname(card->host), csd_struct);
  563. mmc_card_set_bad(card);
  564. return;
  565. }
  566. csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
  567. m = UNSTUFF_BITS(resp, 115, 4);
  568. e = UNSTUFF_BITS(resp, 112, 3);
  569. csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
  570. csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
  571. m = UNSTUFF_BITS(resp, 99, 4);
  572. e = UNSTUFF_BITS(resp, 96, 3);
  573. csd->max_dtr = tran_exp[e] * tran_mant[m];
  574. csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
  575. e = UNSTUFF_BITS(resp, 47, 3);
  576. m = UNSTUFF_BITS(resp, 62, 12);
  577. csd->capacity = (1 + m) << (e + 2);
  578. csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
  579. csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
  580. csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
  581. csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
  582. csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
  583. csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
  584. csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
  585. }
  586. }
  587. /*
  588. * Given a 64-bit response, decode to our card SCR structure.
  589. */
  590. static void mmc_decode_scr(struct mmc_card *card)
  591. {
  592. struct sd_scr *scr = &card->scr;
  593. unsigned int scr_struct;
  594. u32 resp[4];
  595. BUG_ON(!mmc_card_sd(card));
  596. resp[3] = card->raw_scr[1];
  597. resp[2] = card->raw_scr[0];
  598. scr_struct = UNSTUFF_BITS(resp, 60, 4);
  599. if (scr_struct != 0) {
  600. printk("%s: unrecognised SCR structure version %d\n",
  601. mmc_hostname(card->host), scr_struct);
  602. mmc_card_set_bad(card);
  603. return;
  604. }
  605. scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
  606. scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
  607. }
  608. /*
  609. * Locate a MMC card on this MMC host given a raw CID.
  610. */
  611. static struct mmc_card *mmc_find_card(struct mmc_host *host, u32 *raw_cid)
  612. {
  613. struct mmc_card *card;
  614. list_for_each_entry(card, &host->cards, node) {
  615. if (memcmp(card->raw_cid, raw_cid, sizeof(card->raw_cid)) == 0)
  616. return card;
  617. }
  618. return NULL;
  619. }
  620. /*
  621. * Allocate a new MMC card, and assign a unique RCA.
  622. */
  623. static struct mmc_card *
  624. mmc_alloc_card(struct mmc_host *host, u32 *raw_cid, unsigned int *frca)
  625. {
  626. struct mmc_card *card, *c;
  627. unsigned int rca = *frca;
  628. card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL);
  629. if (!card)
  630. return ERR_PTR(-ENOMEM);
  631. mmc_init_card(card, host);
  632. memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid));
  633. again:
  634. list_for_each_entry(c, &host->cards, node)
  635. if (c->rca == rca) {
  636. rca++;
  637. goto again;
  638. }
  639. card->rca = rca;
  640. *frca = rca;
  641. return card;
  642. }
  643. /*
  644. * Tell attached cards to go to IDLE state
  645. */
  646. static void mmc_idle_cards(struct mmc_host *host)
  647. {
  648. struct mmc_command cmd;
  649. host->ios.chip_select = MMC_CS_HIGH;
  650. mmc_set_ios(host);
  651. mmc_delay(1);
  652. cmd.opcode = MMC_GO_IDLE_STATE;
  653. cmd.arg = 0;
  654. cmd.flags = MMC_RSP_NONE | MMC_CMD_BC;
  655. mmc_wait_for_cmd(host, &cmd, 0);
  656. mmc_delay(1);
  657. host->ios.chip_select = MMC_CS_DONTCARE;
  658. mmc_set_ios(host);
  659. mmc_delay(1);
  660. }
  661. /*
  662. * Apply power to the MMC stack. This is a two-stage process.
  663. * First, we enable power to the card without the clock running.
  664. * We then wait a bit for the power to stabilise. Finally,
  665. * enable the bus drivers and clock to the card.
  666. *
  667. * We must _NOT_ enable the clock prior to power stablising.
  668. *
  669. * If a host does all the power sequencing itself, ignore the
  670. * initial MMC_POWER_UP stage.
  671. */
  672. static void mmc_power_up(struct mmc_host *host)
  673. {
  674. int bit = fls(host->ocr_avail) - 1;
  675. host->ios.vdd = bit;
  676. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  677. host->ios.chip_select = MMC_CS_DONTCARE;
  678. host->ios.power_mode = MMC_POWER_UP;
  679. host->ios.bus_width = MMC_BUS_WIDTH_1;
  680. host->ios.timing = MMC_TIMING_LEGACY;
  681. mmc_set_ios(host);
  682. mmc_delay(1);
  683. host->ios.clock = host->f_min;
  684. host->ios.power_mode = MMC_POWER_ON;
  685. mmc_set_ios(host);
  686. mmc_delay(2);
  687. }
  688. static void mmc_power_off(struct mmc_host *host)
  689. {
  690. host->ios.clock = 0;
  691. host->ios.vdd = 0;
  692. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  693. host->ios.chip_select = MMC_CS_DONTCARE;
  694. host->ios.power_mode = MMC_POWER_OFF;
  695. host->ios.bus_width = MMC_BUS_WIDTH_1;
  696. host->ios.timing = MMC_TIMING_LEGACY;
  697. mmc_set_ios(host);
  698. }
  699. static int mmc_send_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
  700. {
  701. struct mmc_command cmd;
  702. int i, err = 0;
  703. cmd.opcode = MMC_SEND_OP_COND;
  704. cmd.arg = ocr;
  705. cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
  706. for (i = 100; i; i--) {
  707. err = mmc_wait_for_cmd(host, &cmd, 0);
  708. if (err != MMC_ERR_NONE)
  709. break;
  710. if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
  711. break;
  712. err = MMC_ERR_TIMEOUT;
  713. mmc_delay(10);
  714. }
  715. if (rocr)
  716. *rocr = cmd.resp[0];
  717. return err;
  718. }
  719. static int mmc_send_app_op_cond(struct mmc_host *host, u32 ocr, u32 *rocr)
  720. {
  721. struct mmc_command cmd;
  722. int i, err = 0;
  723. cmd.opcode = SD_APP_OP_COND;
  724. cmd.arg = ocr;
  725. cmd.flags = MMC_RSP_R3 | MMC_CMD_BCR;
  726. for (i = 100; i; i--) {
  727. err = mmc_wait_for_app_cmd(host, 0, &cmd, CMD_RETRIES);
  728. if (err != MMC_ERR_NONE)
  729. break;
  730. if (cmd.resp[0] & MMC_CARD_BUSY || ocr == 0)
  731. break;
  732. err = MMC_ERR_TIMEOUT;
  733. mmc_delay(10);
  734. }
  735. if (rocr)
  736. *rocr = cmd.resp[0];
  737. return err;
  738. }
  739. static int mmc_send_if_cond(struct mmc_host *host, u32 ocr, int *rsd2)
  740. {
  741. struct mmc_command cmd;
  742. int err, sd2;
  743. static const u8 test_pattern = 0xAA;
  744. /*
  745. * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND
  746. * before SD_APP_OP_COND. This command will harmlessly fail for
  747. * SD 1.0 cards.
  748. */
  749. cmd.opcode = SD_SEND_IF_COND;
  750. cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | test_pattern;
  751. cmd.flags = MMC_RSP_R7 | MMC_CMD_BCR;
  752. err = mmc_wait_for_cmd(host, &cmd, 0);
  753. if (err == MMC_ERR_NONE) {
  754. if ((cmd.resp[0] & 0xFF) == test_pattern) {
  755. sd2 = 1;
  756. } else {
  757. sd2 = 0;
  758. err = MMC_ERR_FAILED;
  759. }
  760. } else {
  761. /*
  762. * Treat errors as SD 1.0 card.
  763. */
  764. sd2 = 0;
  765. err = MMC_ERR_NONE;
  766. }
  767. if (rsd2)
  768. *rsd2 = sd2;
  769. return err;
  770. }
  771. /*
  772. * Discover cards by requesting their CID. If this command
  773. * times out, it is not an error; there are no further cards
  774. * to be discovered. Add new cards to the list.
  775. *
  776. * Create a mmc_card entry for each discovered card, assigning
  777. * it an RCA, and save the raw CID for decoding later.
  778. */
  779. static void mmc_discover_cards(struct mmc_host *host)
  780. {
  781. struct mmc_card *card;
  782. unsigned int first_rca = 1, err;
  783. while (1) {
  784. struct mmc_command cmd;
  785. cmd.opcode = MMC_ALL_SEND_CID;
  786. cmd.arg = 0;
  787. cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
  788. err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
  789. if (err == MMC_ERR_TIMEOUT) {
  790. err = MMC_ERR_NONE;
  791. break;
  792. }
  793. if (err != MMC_ERR_NONE) {
  794. printk(KERN_ERR "%s: error requesting CID: %d\n",
  795. mmc_hostname(host), err);
  796. break;
  797. }
  798. card = mmc_find_card(host, cmd.resp);
  799. if (!card) {
  800. card = mmc_alloc_card(host, cmd.resp, &first_rca);
  801. if (IS_ERR(card)) {
  802. err = PTR_ERR(card);
  803. break;
  804. }
  805. list_add(&card->node, &host->cards);
  806. }
  807. card->state &= ~MMC_STATE_DEAD;
  808. if (host->mode == MMC_MODE_SD) {
  809. mmc_card_set_sd(card);
  810. cmd.opcode = SD_SEND_RELATIVE_ADDR;
  811. cmd.arg = 0;
  812. cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
  813. err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
  814. if (err != MMC_ERR_NONE)
  815. mmc_card_set_dead(card);
  816. else {
  817. card->rca = cmd.resp[0] >> 16;
  818. if (!host->ops->get_ro) {
  819. printk(KERN_WARNING "%s: host does not "
  820. "support reading read-only "
  821. "switch. assuming write-enable.\n",
  822. mmc_hostname(host));
  823. } else {
  824. if (host->ops->get_ro(host))
  825. mmc_card_set_readonly(card);
  826. }
  827. }
  828. } else {
  829. cmd.opcode = MMC_SET_RELATIVE_ADDR;
  830. cmd.arg = card->rca << 16;
  831. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  832. err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
  833. if (err != MMC_ERR_NONE)
  834. mmc_card_set_dead(card);
  835. }
  836. }
  837. }
  838. static void mmc_read_csds(struct mmc_host *host)
  839. {
  840. struct mmc_card *card;
  841. list_for_each_entry(card, &host->cards, node) {
  842. struct mmc_command cmd;
  843. int err;
  844. if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
  845. continue;
  846. cmd.opcode = MMC_SEND_CSD;
  847. cmd.arg = card->rca << 16;
  848. cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
  849. err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
  850. if (err != MMC_ERR_NONE) {
  851. mmc_card_set_dead(card);
  852. continue;
  853. }
  854. memcpy(card->raw_csd, cmd.resp, sizeof(card->raw_csd));
  855. mmc_decode_csd(card);
  856. mmc_decode_cid(card);
  857. }
  858. }
  859. static void mmc_process_ext_csds(struct mmc_host *host)
  860. {
  861. int err;
  862. struct mmc_card *card;
  863. struct mmc_request mrq;
  864. struct mmc_command cmd;
  865. struct mmc_data data;
  866. struct scatterlist sg;
  867. /*
  868. * As the ext_csd is so large and mostly unused, we don't store the
  869. * raw block in mmc_card.
  870. */
  871. u8 *ext_csd;
  872. ext_csd = kmalloc(512, GFP_KERNEL);
  873. if (!ext_csd) {
  874. printk("%s: could not allocate a buffer to receive the ext_csd."
  875. "mmc v4 cards will be treated as v3.\n",
  876. mmc_hostname(host));
  877. return;
  878. }
  879. list_for_each_entry(card, &host->cards, node) {
  880. if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
  881. continue;
  882. if (mmc_card_sd(card))
  883. continue;
  884. if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
  885. continue;
  886. err = mmc_select_card(host, card);
  887. if (err != MMC_ERR_NONE) {
  888. mmc_card_set_dead(card);
  889. continue;
  890. }
  891. memset(&cmd, 0, sizeof(struct mmc_command));
  892. cmd.opcode = MMC_SEND_EXT_CSD;
  893. cmd.arg = 0;
  894. cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  895. memset(&data, 0, sizeof(struct mmc_data));
  896. mmc_set_data_timeout(&data, card, 0);
  897. data.blksz = 512;
  898. data.blocks = 1;
  899. data.flags = MMC_DATA_READ;
  900. data.sg = &sg;
  901. data.sg_len = 1;
  902. memset(&mrq, 0, sizeof(struct mmc_request));
  903. mrq.cmd = &cmd;
  904. mrq.data = &data;
  905. sg_init_one(&sg, ext_csd, 512);
  906. mmc_wait_for_req(host, &mrq);
  907. if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
  908. printk("%s: unable to read EXT_CSD, performance "
  909. "might suffer.\n", mmc_hostname(card->host));
  910. continue;
  911. }
  912. switch (ext_csd[EXT_CSD_CARD_TYPE]) {
  913. case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
  914. card->ext_csd.hs_max_dtr = 52000000;
  915. break;
  916. case EXT_CSD_CARD_TYPE_26:
  917. card->ext_csd.hs_max_dtr = 26000000;
  918. break;
  919. default:
  920. /* MMC v4 spec says this cannot happen */
  921. printk("%s: card is mmc v4 but doesn't support "
  922. "any high-speed modes.\n",
  923. mmc_hostname(card->host));
  924. continue;
  925. }
  926. if (host->caps & MMC_CAP_MMC_HIGHSPEED) {
  927. /* Activate highspeed support. */
  928. cmd.opcode = MMC_SWITCH;
  929. cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
  930. (EXT_CSD_HS_TIMING << 16) |
  931. (1 << 8) |
  932. EXT_CSD_CMD_SET_NORMAL;
  933. cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
  934. err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
  935. if (err != MMC_ERR_NONE) {
  936. printk("%s: failed to switch card to mmc v4 "
  937. "high-speed mode.\n",
  938. mmc_hostname(card->host));
  939. continue;
  940. }
  941. mmc_card_set_highspeed(card);
  942. host->ios.timing = MMC_TIMING_MMC_HS;
  943. mmc_set_ios(host);
  944. }
  945. /* Check for host support for wide-bus modes. */
  946. if (host->caps & MMC_CAP_4_BIT_DATA) {
  947. /* Activate 4-bit support. */
  948. cmd.opcode = MMC_SWITCH;
  949. cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
  950. (EXT_CSD_BUS_WIDTH << 16) |
  951. (EXT_CSD_BUS_WIDTH_4 << 8) |
  952. EXT_CSD_CMD_SET_NORMAL;
  953. cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
  954. err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
  955. if (err != MMC_ERR_NONE) {
  956. printk("%s: failed to switch card to "
  957. "mmc v4 4-bit bus mode.\n",
  958. mmc_hostname(card->host));
  959. continue;
  960. }
  961. host->ios.bus_width = MMC_BUS_WIDTH_4;
  962. mmc_set_ios(host);
  963. }
  964. }
  965. kfree(ext_csd);
  966. mmc_deselect_cards(host);
  967. }
  968. static void mmc_read_scrs(struct mmc_host *host)
  969. {
  970. int err;
  971. struct mmc_card *card;
  972. struct mmc_request mrq;
  973. struct mmc_command cmd;
  974. struct mmc_data data;
  975. struct scatterlist sg;
  976. list_for_each_entry(card, &host->cards, node) {
  977. if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
  978. continue;
  979. if (!mmc_card_sd(card))
  980. continue;
  981. err = mmc_select_card(host, card);
  982. if (err != MMC_ERR_NONE) {
  983. mmc_card_set_dead(card);
  984. continue;
  985. }
  986. memset(&cmd, 0, sizeof(struct mmc_command));
  987. cmd.opcode = MMC_APP_CMD;
  988. cmd.arg = card->rca << 16;
  989. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  990. err = mmc_wait_for_cmd(host, &cmd, 0);
  991. if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD)) {
  992. mmc_card_set_dead(card);
  993. continue;
  994. }
  995. memset(&cmd, 0, sizeof(struct mmc_command));
  996. cmd.opcode = SD_APP_SEND_SCR;
  997. cmd.arg = 0;
  998. cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  999. memset(&data, 0, sizeof(struct mmc_data));
  1000. mmc_set_data_timeout(&data, card, 0);
  1001. data.blksz = 1 << 3;
  1002. data.blocks = 1;
  1003. data.flags = MMC_DATA_READ;
  1004. data.sg = &sg;
  1005. data.sg_len = 1;
  1006. memset(&mrq, 0, sizeof(struct mmc_request));
  1007. mrq.cmd = &cmd;
  1008. mrq.data = &data;
  1009. sg_init_one(&sg, (u8*)card->raw_scr, 8);
  1010. mmc_wait_for_req(host, &mrq);
  1011. if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
  1012. mmc_card_set_dead(card);
  1013. continue;
  1014. }
  1015. card->raw_scr[0] = ntohl(card->raw_scr[0]);
  1016. card->raw_scr[1] = ntohl(card->raw_scr[1]);
  1017. mmc_decode_scr(card);
  1018. }
  1019. mmc_deselect_cards(host);
  1020. }
  1021. static void mmc_read_switch_caps(struct mmc_host *host)
  1022. {
  1023. int err;
  1024. struct mmc_card *card;
  1025. struct mmc_request mrq;
  1026. struct mmc_command cmd;
  1027. struct mmc_data data;
  1028. unsigned char *status;
  1029. struct scatterlist sg;
  1030. if (!(host->caps & MMC_CAP_SD_HIGHSPEED))
  1031. return;
  1032. status = kmalloc(64, GFP_KERNEL);
  1033. if (!status) {
  1034. printk(KERN_WARNING "%s: Unable to allocate buffer for "
  1035. "reading switch capabilities.\n",
  1036. mmc_hostname(host));
  1037. return;
  1038. }
  1039. list_for_each_entry(card, &host->cards, node) {
  1040. if (card->state & (MMC_STATE_DEAD|MMC_STATE_PRESENT))
  1041. continue;
  1042. if (!mmc_card_sd(card))
  1043. continue;
  1044. if (card->scr.sda_vsn < SCR_SPEC_VER_1)
  1045. continue;
  1046. err = mmc_select_card(host, card);
  1047. if (err != MMC_ERR_NONE) {
  1048. mmc_card_set_dead(card);
  1049. continue;
  1050. }
  1051. memset(&cmd, 0, sizeof(struct mmc_command));
  1052. cmd.opcode = SD_SWITCH;
  1053. cmd.arg = 0x00FFFFF1;
  1054. cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  1055. memset(&data, 0, sizeof(struct mmc_data));
  1056. mmc_set_data_timeout(&data, card, 0);
  1057. data.blksz = 64;
  1058. data.blocks = 1;
  1059. data.flags = MMC_DATA_READ;
  1060. data.sg = &sg;
  1061. data.sg_len = 1;
  1062. memset(&mrq, 0, sizeof(struct mmc_request));
  1063. mrq.cmd = &cmd;
  1064. mrq.data = &data;
  1065. sg_init_one(&sg, status, 64);
  1066. mmc_wait_for_req(host, &mrq);
  1067. if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE) {
  1068. printk("%s: unable to read switch capabilities, "
  1069. "performance might suffer.\n",
  1070. mmc_hostname(card->host));
  1071. continue;
  1072. }
  1073. if (status[13] & 0x02)
  1074. card->sw_caps.hs_max_dtr = 50000000;
  1075. memset(&cmd, 0, sizeof(struct mmc_command));
  1076. cmd.opcode = SD_SWITCH;
  1077. cmd.arg = 0x80FFFFF1;
  1078. cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  1079. memset(&data, 0, sizeof(struct mmc_data));
  1080. mmc_set_data_timeout(&data, card, 0);
  1081. data.blksz = 64;
  1082. data.blocks = 1;
  1083. data.flags = MMC_DATA_READ;
  1084. data.sg = &sg;
  1085. data.sg_len = 1;
  1086. memset(&mrq, 0, sizeof(struct mmc_request));
  1087. mrq.cmd = &cmd;
  1088. mrq.data = &data;
  1089. sg_init_one(&sg, status, 64);
  1090. mmc_wait_for_req(host, &mrq);
  1091. if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE ||
  1092. (status[16] & 0xF) != 1) {
  1093. printk(KERN_WARNING "%s: Problem switching card "
  1094. "into high-speed mode!\n",
  1095. mmc_hostname(host));
  1096. continue;
  1097. }
  1098. mmc_card_set_highspeed(card);
  1099. host->ios.timing = MMC_TIMING_SD_HS;
  1100. mmc_set_ios(host);
  1101. }
  1102. kfree(status);
  1103. mmc_deselect_cards(host);
  1104. }
  1105. static unsigned int mmc_calculate_clock(struct mmc_host *host)
  1106. {
  1107. struct mmc_card *card;
  1108. unsigned int max_dtr = host->f_max;
  1109. list_for_each_entry(card, &host->cards, node)
  1110. if (!mmc_card_dead(card)) {
  1111. if (mmc_card_highspeed(card) && mmc_card_sd(card)) {
  1112. if (max_dtr > card->sw_caps.hs_max_dtr)
  1113. max_dtr = card->sw_caps.hs_max_dtr;
  1114. } else if (mmc_card_highspeed(card) && !mmc_card_sd(card)) {
  1115. if (max_dtr > card->ext_csd.hs_max_dtr)
  1116. max_dtr = card->ext_csd.hs_max_dtr;
  1117. } else if (max_dtr > card->csd.max_dtr) {
  1118. max_dtr = card->csd.max_dtr;
  1119. }
  1120. }
  1121. pr_debug("%s: selected %d.%03dMHz transfer rate\n",
  1122. mmc_hostname(host),
  1123. max_dtr / 1000000, (max_dtr / 1000) % 1000);
  1124. return max_dtr;
  1125. }
  1126. /*
  1127. * Check whether cards we already know about are still present.
  1128. * We do this by requesting status, and checking whether a card
  1129. * responds.
  1130. *
  1131. * A request for status does not cause a state change in data
  1132. * transfer mode.
  1133. */
  1134. static void mmc_check_cards(struct mmc_host *host)
  1135. {
  1136. struct list_head *l, *n;
  1137. mmc_deselect_cards(host);
  1138. list_for_each_safe(l, n, &host->cards) {
  1139. struct mmc_card *card = mmc_list_to_card(l);
  1140. struct mmc_command cmd;
  1141. int err;
  1142. cmd.opcode = MMC_SEND_STATUS;
  1143. cmd.arg = card->rca << 16;
  1144. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  1145. err = mmc_wait_for_cmd(host, &cmd, CMD_RETRIES);
  1146. if (err == MMC_ERR_NONE)
  1147. continue;
  1148. mmc_card_set_dead(card);
  1149. }
  1150. }
  1151. static void mmc_setup(struct mmc_host *host)
  1152. {
  1153. if (host->ios.power_mode != MMC_POWER_ON) {
  1154. int err;
  1155. u32 ocr;
  1156. host->mode = MMC_MODE_SD;
  1157. mmc_power_up(host);
  1158. mmc_idle_cards(host);
  1159. err = mmc_send_if_cond(host, host->ocr_avail, NULL);
  1160. if (err != MMC_ERR_NONE) {
  1161. return;
  1162. }
  1163. err = mmc_send_app_op_cond(host, 0, &ocr);
  1164. /*
  1165. * If we fail to detect any SD cards then try
  1166. * searching for MMC cards.
  1167. */
  1168. if (err != MMC_ERR_NONE) {
  1169. host->mode = MMC_MODE_MMC;
  1170. err = mmc_send_op_cond(host, 0, &ocr);
  1171. if (err != MMC_ERR_NONE)
  1172. return;
  1173. }
  1174. host->ocr = mmc_select_voltage(host, ocr);
  1175. /*
  1176. * Since we're changing the OCR value, we seem to
  1177. * need to tell some cards to go back to the idle
  1178. * state. We wait 1ms to give cards time to
  1179. * respond.
  1180. */
  1181. if (host->ocr)
  1182. mmc_idle_cards(host);
  1183. } else {
  1184. host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
  1185. host->ios.clock = host->f_min;
  1186. mmc_set_ios(host);
  1187. /*
  1188. * We should remember the OCR mask from the existing
  1189. * cards, and detect the new cards OCR mask, combine
  1190. * the two and re-select the VDD. However, if we do
  1191. * change VDD, we should do an idle, and then do a
  1192. * full re-initialisation. We would need to notify
  1193. * drivers so that they can re-setup the cards as
  1194. * well, while keeping their queues at bay.
  1195. *
  1196. * For the moment, we take the easy way out - if the
  1197. * new cards don't like our currently selected VDD,
  1198. * they drop off the bus.
  1199. */
  1200. }
  1201. if (host->ocr == 0)
  1202. return;
  1203. /*
  1204. * Send the selected OCR multiple times... until the cards
  1205. * all get the idea that they should be ready for CMD2.
  1206. * (My SanDisk card seems to need this.)
  1207. */
  1208. if (host->mode == MMC_MODE_SD) {
  1209. int err, sd2;
  1210. err = mmc_send_if_cond(host, host->ocr, &sd2);
  1211. if (err == MMC_ERR_NONE) {
  1212. /*
  1213. * If SD_SEND_IF_COND indicates an SD 2.0
  1214. * compliant card and we should set bit 30
  1215. * of the ocr to indicate that we can handle
  1216. * block-addressed SDHC cards.
  1217. */
  1218. mmc_send_app_op_cond(host, host->ocr | (sd2 << 30), NULL);
  1219. }
  1220. } else {
  1221. mmc_send_op_cond(host, host->ocr, NULL);
  1222. }
  1223. mmc_discover_cards(host);
  1224. /*
  1225. * Ok, now switch to push-pull mode.
  1226. */
  1227. host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
  1228. mmc_set_ios(host);
  1229. mmc_read_csds(host);
  1230. if (host->mode == MMC_MODE_SD) {
  1231. mmc_read_scrs(host);
  1232. mmc_read_switch_caps(host);
  1233. } else
  1234. mmc_process_ext_csds(host);
  1235. }
  1236. /**
  1237. * mmc_detect_change - process change of state on a MMC socket
  1238. * @host: host which changed state.
  1239. * @delay: optional delay to wait before detection (jiffies)
  1240. *
  1241. * All we know is that card(s) have been inserted or removed
  1242. * from the socket(s). We don't know which socket or cards.
  1243. */
  1244. void mmc_detect_change(struct mmc_host *host, unsigned long delay)
  1245. {
  1246. mmc_schedule_delayed_work(&host->detect, delay);
  1247. }
  1248. EXPORT_SYMBOL(mmc_detect_change);
  1249. static void mmc_rescan(struct work_struct *work)
  1250. {
  1251. struct mmc_host *host =
  1252. container_of(work, struct mmc_host, detect.work);
  1253. struct list_head *l, *n;
  1254. unsigned char power_mode;
  1255. mmc_claim_host(host);
  1256. /*
  1257. * Check for removed cards and newly inserted ones. We check for
  1258. * removed cards first so we can intelligently re-select the VDD.
  1259. */
  1260. power_mode = host->ios.power_mode;
  1261. if (power_mode == MMC_POWER_ON)
  1262. mmc_check_cards(host);
  1263. mmc_setup(host);
  1264. /*
  1265. * Some broken cards process CMD1 even in stand-by state. There is
  1266. * no reply, but an ILLEGAL_COMMAND error is cached and returned
  1267. * after next command. We poll for card status here to clear any
  1268. * possibly pending error.
  1269. */
  1270. if (power_mode == MMC_POWER_ON)
  1271. mmc_check_cards(host);
  1272. if (!list_empty(&host->cards)) {
  1273. /*
  1274. * (Re-)calculate the fastest clock rate which the
  1275. * attached cards and the host support.
  1276. */
  1277. host->ios.clock = mmc_calculate_clock(host);
  1278. mmc_set_ios(host);
  1279. }
  1280. mmc_release_host(host);
  1281. list_for_each_safe(l, n, &host->cards) {
  1282. struct mmc_card *card = mmc_list_to_card(l);
  1283. /*
  1284. * If this is a new and good card, register it.
  1285. */
  1286. if (!mmc_card_present(card) && !mmc_card_dead(card)) {
  1287. if (mmc_register_card(card))
  1288. mmc_card_set_dead(card);
  1289. else
  1290. mmc_card_set_present(card);
  1291. }
  1292. /*
  1293. * If this card is dead, destroy it.
  1294. */
  1295. if (mmc_card_dead(card)) {
  1296. list_del(&card->node);
  1297. mmc_remove_card(card);
  1298. }
  1299. }
  1300. /*
  1301. * If we discover that there are no cards on the
  1302. * bus, turn off the clock and power down.
  1303. */
  1304. if (list_empty(&host->cards))
  1305. mmc_power_off(host);
  1306. }
  1307. /**
  1308. * mmc_alloc_host - initialise the per-host structure.
  1309. * @extra: sizeof private data structure
  1310. * @dev: pointer to host device model structure
  1311. *
  1312. * Initialise the per-host structure.
  1313. */
  1314. struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
  1315. {
  1316. struct mmc_host *host;
  1317. host = mmc_alloc_host_sysfs(extra, dev);
  1318. if (host) {
  1319. spin_lock_init(&host->lock);
  1320. init_waitqueue_head(&host->wq);
  1321. INIT_LIST_HEAD(&host->cards);
  1322. INIT_DELAYED_WORK(&host->detect, mmc_rescan);
  1323. /*
  1324. * By default, hosts do not support SGIO or large requests.
  1325. * They have to set these according to their abilities.
  1326. */
  1327. host->max_hw_segs = 1;
  1328. host->max_phys_segs = 1;
  1329. host->max_seg_size = PAGE_CACHE_SIZE;
  1330. host->max_req_size = PAGE_CACHE_SIZE;
  1331. host->max_blk_size = 512;
  1332. host->max_blk_count = PAGE_CACHE_SIZE / 512;
  1333. }
  1334. return host;
  1335. }
  1336. EXPORT_SYMBOL(mmc_alloc_host);
  1337. /**
  1338. * mmc_add_host - initialise host hardware
  1339. * @host: mmc host
  1340. */
  1341. int mmc_add_host(struct mmc_host *host)
  1342. {
  1343. int ret;
  1344. ret = mmc_add_host_sysfs(host);
  1345. if (ret == 0) {
  1346. mmc_power_off(host);
  1347. mmc_detect_change(host, 0);
  1348. }
  1349. return ret;
  1350. }
  1351. EXPORT_SYMBOL(mmc_add_host);
  1352. /**
  1353. * mmc_remove_host - remove host hardware
  1354. * @host: mmc host
  1355. *
  1356. * Unregister and remove all cards associated with this host,
  1357. * and power down the MMC bus.
  1358. */
  1359. void mmc_remove_host(struct mmc_host *host)
  1360. {
  1361. struct list_head *l, *n;
  1362. list_for_each_safe(l, n, &host->cards) {
  1363. struct mmc_card *card = mmc_list_to_card(l);
  1364. mmc_remove_card(card);
  1365. }
  1366. mmc_power_off(host);
  1367. mmc_remove_host_sysfs(host);
  1368. }
  1369. EXPORT_SYMBOL(mmc_remove_host);
  1370. /**
  1371. * mmc_free_host - free the host structure
  1372. * @host: mmc host
  1373. *
  1374. * Free the host once all references to it have been dropped.
  1375. */
  1376. void mmc_free_host(struct mmc_host *host)
  1377. {
  1378. mmc_flush_scheduled_work();
  1379. mmc_free_host_sysfs(host);
  1380. }
  1381. EXPORT_SYMBOL(mmc_free_host);
  1382. #ifdef CONFIG_PM
  1383. /**
  1384. * mmc_suspend_host - suspend a host
  1385. * @host: mmc host
  1386. * @state: suspend mode (PM_SUSPEND_xxx)
  1387. */
  1388. int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
  1389. {
  1390. mmc_claim_host(host);
  1391. mmc_deselect_cards(host);
  1392. mmc_power_off(host);
  1393. mmc_release_host(host);
  1394. return 0;
  1395. }
  1396. EXPORT_SYMBOL(mmc_suspend_host);
  1397. /**
  1398. * mmc_resume_host - resume a previously suspended host
  1399. * @host: mmc host
  1400. */
  1401. int mmc_resume_host(struct mmc_host *host)
  1402. {
  1403. mmc_rescan(&host->detect.work);
  1404. return 0;
  1405. }
  1406. EXPORT_SYMBOL(mmc_resume_host);
  1407. #endif
  1408. MODULE_LICENSE("GPL");