mmc.c 39 KB

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