mmc.c 39 KB

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