mmc.c 39 KB

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