mmc_spi.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554
  1. /*
  2. * mmc_spi.c - Access SD/MMC cards through SPI master controllers
  3. *
  4. * (C) Copyright 2005, Intec Automation,
  5. * Mike Lavender (mike@steroidmicros)
  6. * (C) Copyright 2006-2007, David Brownell
  7. * (C) Copyright 2007, Axis Communications,
  8. * Hans-Peter Nilsson (hp@axis.com)
  9. * (C) Copyright 2007, ATRON electronic GmbH,
  10. * Jan Nikitenko <jan.nikitenko@gmail.com>
  11. *
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. */
  27. #include <linux/sched.h>
  28. #include <linux/delay.h>
  29. #include <linux/slab.h>
  30. #include <linux/bio.h>
  31. #include <linux/dma-mapping.h>
  32. #include <linux/crc7.h>
  33. #include <linux/crc-itu-t.h>
  34. #include <linux/scatterlist.h>
  35. #include <linux/mmc/host.h>
  36. #include <linux/mmc/mmc.h> /* for R1_SPI_* bit values */
  37. #include <linux/spi/spi.h>
  38. #include <linux/spi/mmc_spi.h>
  39. #include <asm/unaligned.h>
  40. /* NOTES:
  41. *
  42. * - For now, we won't try to interoperate with a real mmc/sd/sdio
  43. * controller, although some of them do have hardware support for
  44. * SPI protocol. The main reason for such configs would be mmc-ish
  45. * cards like DataFlash, which don't support that "native" protocol.
  46. *
  47. * We don't have a "DataFlash/MMC/SD/SDIO card slot" abstraction to
  48. * switch between driver stacks, and in any case if "native" mode
  49. * is available, it will be faster and hence preferable.
  50. *
  51. * - MMC depends on a different chipselect management policy than the
  52. * SPI interface currently supports for shared bus segments: it needs
  53. * to issue multiple spi_message requests with the chipselect active,
  54. * using the results of one message to decide the next one to issue.
  55. *
  56. * Pending updates to the programming interface, this driver expects
  57. * that it not share the bus with other drivers (precluding conflicts).
  58. *
  59. * - We tell the controller to keep the chipselect active from the
  60. * beginning of an mmc_host_ops.request until the end. So beware
  61. * of SPI controller drivers that mis-handle the cs_change flag!
  62. *
  63. * However, many cards seem OK with chipselect flapping up/down
  64. * during that time ... at least on unshared bus segments.
  65. */
  66. /*
  67. * Local protocol constants, internal to data block protocols.
  68. */
  69. /* Response tokens used to ack each block written: */
  70. #define SPI_MMC_RESPONSE_CODE(x) ((x) & 0x1f)
  71. #define SPI_RESPONSE_ACCEPTED ((2 << 1)|1)
  72. #define SPI_RESPONSE_CRC_ERR ((5 << 1)|1)
  73. #define SPI_RESPONSE_WRITE_ERR ((6 << 1)|1)
  74. /* Read and write blocks start with these tokens and end with crc;
  75. * on error, read tokens act like a subset of R2_SPI_* values.
  76. */
  77. #define SPI_TOKEN_SINGLE 0xfe /* single block r/w, multiblock read */
  78. #define SPI_TOKEN_MULTI_WRITE 0xfc /* multiblock write */
  79. #define SPI_TOKEN_STOP_TRAN 0xfd /* terminate multiblock write */
  80. #define MMC_SPI_BLOCKSIZE 512
  81. /* These fixed timeouts come from the latest SD specs, which say to ignore
  82. * the CSD values. The R1B value is for card erase (e.g. the "I forgot the
  83. * card's password" scenario); it's mostly applied to STOP_TRANSMISSION after
  84. * reads which takes nowhere near that long. Older cards may be able to use
  85. * shorter timeouts ... but why bother?
  86. */
  87. #define r1b_timeout (HZ * 3)
  88. /* One of the critical speed parameters is the amount of data which may
  89. * be transferred in one command. If this value is too low, the SD card
  90. * controller has to do multiple partial block writes (argggh!). With
  91. * today (2008) SD cards there is little speed gain if we transfer more
  92. * than 64 KBytes at a time. So use this value until there is any indication
  93. * that we should do more here.
  94. */
  95. #define MMC_SPI_BLOCKSATONCE 128
  96. /****************************************************************************/
  97. /*
  98. * Local Data Structures
  99. */
  100. /* "scratch" is per-{command,block} data exchanged with the card */
  101. struct scratch {
  102. u8 status[29];
  103. u8 data_token;
  104. __be16 crc_val;
  105. };
  106. struct mmc_spi_host {
  107. struct mmc_host *mmc;
  108. struct spi_device *spi;
  109. unsigned char power_mode;
  110. u16 powerup_msecs;
  111. struct mmc_spi_platform_data *pdata;
  112. /* for bulk data transfers */
  113. struct spi_transfer token, t, crc, early_status;
  114. struct spi_message m;
  115. /* for status readback */
  116. struct spi_transfer status;
  117. struct spi_message readback;
  118. /* underlying DMA-aware controller, or null */
  119. struct device *dma_dev;
  120. /* buffer used for commands and for message "overhead" */
  121. struct scratch *data;
  122. dma_addr_t data_dma;
  123. /* Specs say to write ones most of the time, even when the card
  124. * has no need to read its input data; and many cards won't care.
  125. * This is our source of those ones.
  126. */
  127. void *ones;
  128. dma_addr_t ones_dma;
  129. };
  130. /****************************************************************************/
  131. /*
  132. * MMC-over-SPI protocol glue, used by the MMC stack interface
  133. */
  134. static inline int mmc_cs_off(struct mmc_spi_host *host)
  135. {
  136. /* chipselect will always be inactive after setup() */
  137. return spi_setup(host->spi);
  138. }
  139. static int
  140. mmc_spi_readbytes(struct mmc_spi_host *host, unsigned len)
  141. {
  142. int status;
  143. if (len > sizeof(*host->data)) {
  144. WARN_ON(1);
  145. return -EIO;
  146. }
  147. host->status.len = len;
  148. if (host->dma_dev)
  149. dma_sync_single_for_device(host->dma_dev,
  150. host->data_dma, sizeof(*host->data),
  151. DMA_FROM_DEVICE);
  152. status = spi_sync_locked(host->spi, &host->readback);
  153. if (host->dma_dev)
  154. dma_sync_single_for_cpu(host->dma_dev,
  155. host->data_dma, sizeof(*host->data),
  156. DMA_FROM_DEVICE);
  157. return status;
  158. }
  159. static int mmc_spi_skip(struct mmc_spi_host *host, unsigned long timeout,
  160. unsigned n, u8 byte)
  161. {
  162. u8 *cp = host->data->status;
  163. unsigned long start = jiffies;
  164. while (1) {
  165. int status;
  166. unsigned i;
  167. status = mmc_spi_readbytes(host, n);
  168. if (status < 0)
  169. return status;
  170. for (i = 0; i < n; i++) {
  171. if (cp[i] != byte)
  172. return cp[i];
  173. }
  174. if (time_is_before_jiffies(start + timeout))
  175. break;
  176. /* If we need long timeouts, we may release the CPU.
  177. * We use jiffies here because we want to have a relation
  178. * between elapsed time and the blocking of the scheduler.
  179. */
  180. if (time_is_before_jiffies(start+1))
  181. schedule();
  182. }
  183. return -ETIMEDOUT;
  184. }
  185. static inline int
  186. mmc_spi_wait_unbusy(struct mmc_spi_host *host, unsigned long timeout)
  187. {
  188. return mmc_spi_skip(host, timeout, sizeof(host->data->status), 0);
  189. }
  190. static int mmc_spi_readtoken(struct mmc_spi_host *host, unsigned long timeout)
  191. {
  192. return mmc_spi_skip(host, timeout, 1, 0xff);
  193. }
  194. /*
  195. * Note that for SPI, cmd->resp[0] is not the same data as "native" protocol
  196. * hosts return! The low byte holds R1_SPI bits. The next byte may hold
  197. * R2_SPI bits ... for SEND_STATUS, or after data read errors.
  198. *
  199. * cmd->resp[1] holds any four-byte response, for R3 (READ_OCR) and on
  200. * newer cards R7 (IF_COND).
  201. */
  202. static char *maptype(struct mmc_command *cmd)
  203. {
  204. switch (mmc_spi_resp_type(cmd)) {
  205. case MMC_RSP_SPI_R1: return "R1";
  206. case MMC_RSP_SPI_R1B: return "R1B";
  207. case MMC_RSP_SPI_R2: return "R2/R5";
  208. case MMC_RSP_SPI_R3: return "R3/R4/R7";
  209. default: return "?";
  210. }
  211. }
  212. /* return zero, else negative errno after setting cmd->error */
  213. static int mmc_spi_response_get(struct mmc_spi_host *host,
  214. struct mmc_command *cmd, int cs_on)
  215. {
  216. u8 *cp = host->data->status;
  217. u8 *end = cp + host->t.len;
  218. int value = 0;
  219. int bitshift;
  220. u8 leftover = 0;
  221. unsigned short rotator;
  222. int i;
  223. char tag[32];
  224. snprintf(tag, sizeof(tag), " ... CMD%d response SPI_%s",
  225. cmd->opcode, maptype(cmd));
  226. /* Except for data block reads, the whole response will already
  227. * be stored in the scratch buffer. It's somewhere after the
  228. * command and the first byte we read after it. We ignore that
  229. * first byte. After STOP_TRANSMISSION command it may include
  230. * two data bits, but otherwise it's all ones.
  231. */
  232. cp += 8;
  233. while (cp < end && *cp == 0xff)
  234. cp++;
  235. /* Data block reads (R1 response types) may need more data... */
  236. if (cp == end) {
  237. cp = host->data->status;
  238. end = cp+1;
  239. /* Card sends N(CR) (== 1..8) bytes of all-ones then one
  240. * status byte ... and we already scanned 2 bytes.
  241. *
  242. * REVISIT block read paths use nasty byte-at-a-time I/O
  243. * so it can always DMA directly into the target buffer.
  244. * It'd probably be better to memcpy() the first chunk and
  245. * avoid extra i/o calls...
  246. *
  247. * Note we check for more than 8 bytes, because in practice,
  248. * some SD cards are slow...
  249. */
  250. for (i = 2; i < 16; i++) {
  251. value = mmc_spi_readbytes(host, 1);
  252. if (value < 0)
  253. goto done;
  254. if (*cp != 0xff)
  255. goto checkstatus;
  256. }
  257. value = -ETIMEDOUT;
  258. goto done;
  259. }
  260. checkstatus:
  261. bitshift = 0;
  262. if (*cp & 0x80) {
  263. /* Houston, we have an ugly card with a bit-shifted response */
  264. rotator = *cp++ << 8;
  265. /* read the next byte */
  266. if (cp == end) {
  267. value = mmc_spi_readbytes(host, 1);
  268. if (value < 0)
  269. goto done;
  270. cp = host->data->status;
  271. end = cp+1;
  272. }
  273. rotator |= *cp++;
  274. while (rotator & 0x8000) {
  275. bitshift++;
  276. rotator <<= 1;
  277. }
  278. cmd->resp[0] = rotator >> 8;
  279. leftover = rotator;
  280. } else {
  281. cmd->resp[0] = *cp++;
  282. }
  283. cmd->error = 0;
  284. /* Status byte: the entire seven-bit R1 response. */
  285. if (cmd->resp[0] != 0) {
  286. if ((R1_SPI_PARAMETER | R1_SPI_ADDRESS)
  287. & cmd->resp[0])
  288. value = -EFAULT; /* Bad address */
  289. else if (R1_SPI_ILLEGAL_COMMAND & cmd->resp[0])
  290. value = -ENOSYS; /* Function not implemented */
  291. else if (R1_SPI_COM_CRC & cmd->resp[0])
  292. value = -EILSEQ; /* Illegal byte sequence */
  293. else if ((R1_SPI_ERASE_SEQ | R1_SPI_ERASE_RESET)
  294. & cmd->resp[0])
  295. value = -EIO; /* I/O error */
  296. /* else R1_SPI_IDLE, "it's resetting" */
  297. }
  298. switch (mmc_spi_resp_type(cmd)) {
  299. /* SPI R1B == R1 + busy; STOP_TRANSMISSION (for multiblock reads)
  300. * and less-common stuff like various erase operations.
  301. */
  302. case MMC_RSP_SPI_R1B:
  303. /* maybe we read all the busy tokens already */
  304. while (cp < end && *cp == 0)
  305. cp++;
  306. if (cp == end)
  307. mmc_spi_wait_unbusy(host, r1b_timeout);
  308. break;
  309. /* SPI R2 == R1 + second status byte; SEND_STATUS
  310. * SPI R5 == R1 + data byte; IO_RW_DIRECT
  311. */
  312. case MMC_RSP_SPI_R2:
  313. /* read the next byte */
  314. if (cp == end) {
  315. value = mmc_spi_readbytes(host, 1);
  316. if (value < 0)
  317. goto done;
  318. cp = host->data->status;
  319. end = cp+1;
  320. }
  321. if (bitshift) {
  322. rotator = leftover << 8;
  323. rotator |= *cp << bitshift;
  324. cmd->resp[0] |= (rotator & 0xFF00);
  325. } else {
  326. cmd->resp[0] |= *cp << 8;
  327. }
  328. break;
  329. /* SPI R3, R4, or R7 == R1 + 4 bytes */
  330. case MMC_RSP_SPI_R3:
  331. rotator = leftover << 8;
  332. cmd->resp[1] = 0;
  333. for (i = 0; i < 4; i++) {
  334. cmd->resp[1] <<= 8;
  335. /* read the next byte */
  336. if (cp == end) {
  337. value = mmc_spi_readbytes(host, 1);
  338. if (value < 0)
  339. goto done;
  340. cp = host->data->status;
  341. end = cp+1;
  342. }
  343. if (bitshift) {
  344. rotator |= *cp++ << bitshift;
  345. cmd->resp[1] |= (rotator >> 8);
  346. rotator <<= 8;
  347. } else {
  348. cmd->resp[1] |= *cp++;
  349. }
  350. }
  351. break;
  352. /* SPI R1 == just one status byte */
  353. case MMC_RSP_SPI_R1:
  354. break;
  355. default:
  356. dev_dbg(&host->spi->dev, "bad response type %04x\n",
  357. mmc_spi_resp_type(cmd));
  358. if (value >= 0)
  359. value = -EINVAL;
  360. goto done;
  361. }
  362. if (value < 0)
  363. dev_dbg(&host->spi->dev, "%s: resp %04x %08x\n",
  364. tag, cmd->resp[0], cmd->resp[1]);
  365. /* disable chipselect on errors and some success cases */
  366. if (value >= 0 && cs_on)
  367. return value;
  368. done:
  369. if (value < 0)
  370. cmd->error = value;
  371. mmc_cs_off(host);
  372. return value;
  373. }
  374. /* Issue command and read its response.
  375. * Returns zero on success, negative for error.
  376. *
  377. * On error, caller must cope with mmc core retry mechanism. That
  378. * means immediate low-level resubmit, which affects the bus lock...
  379. */
  380. static int
  381. mmc_spi_command_send(struct mmc_spi_host *host,
  382. struct mmc_request *mrq,
  383. struct mmc_command *cmd, int cs_on)
  384. {
  385. struct scratch *data = host->data;
  386. u8 *cp = data->status;
  387. u32 arg = cmd->arg;
  388. int status;
  389. struct spi_transfer *t;
  390. /* We can handle most commands (except block reads) in one full
  391. * duplex I/O operation before either starting the next transfer
  392. * (data block or command) or else deselecting the card.
  393. *
  394. * First, write 7 bytes:
  395. * - an all-ones byte to ensure the card is ready
  396. * - opcode byte (plus start and transmission bits)
  397. * - four bytes of big-endian argument
  398. * - crc7 (plus end bit) ... always computed, it's cheap
  399. *
  400. * We init the whole buffer to all-ones, which is what we need
  401. * to write while we're reading (later) response data.
  402. */
  403. memset(cp++, 0xff, sizeof(data->status));
  404. *cp++ = 0x40 | cmd->opcode;
  405. *cp++ = (u8)(arg >> 24);
  406. *cp++ = (u8)(arg >> 16);
  407. *cp++ = (u8)(arg >> 8);
  408. *cp++ = (u8)arg;
  409. *cp++ = (crc7(0, &data->status[1], 5) << 1) | 0x01;
  410. /* Then, read up to 13 bytes (while writing all-ones):
  411. * - N(CR) (== 1..8) bytes of all-ones
  412. * - status byte (for all response types)
  413. * - the rest of the response, either:
  414. * + nothing, for R1 or R1B responses
  415. * + second status byte, for R2 responses
  416. * + four data bytes, for R3 and R7 responses
  417. *
  418. * Finally, read some more bytes ... in the nice cases we know in
  419. * advance how many, and reading 1 more is always OK:
  420. * - N(EC) (== 0..N) bytes of all-ones, before deselect/finish
  421. * - N(RC) (== 1..N) bytes of all-ones, before next command
  422. * - N(WR) (== 1..N) bytes of all-ones, before data write
  423. *
  424. * So in those cases one full duplex I/O of at most 21 bytes will
  425. * handle the whole command, leaving the card ready to receive a
  426. * data block or new command. We do that whenever we can, shaving
  427. * CPU and IRQ costs (especially when using DMA or FIFOs).
  428. *
  429. * There are two other cases, where it's not generally practical
  430. * to rely on a single I/O:
  431. *
  432. * - R1B responses need at least N(EC) bytes of all-zeroes.
  433. *
  434. * In this case we can *try* to fit it into one I/O, then
  435. * maybe read more data later.
  436. *
  437. * - Data block reads are more troublesome, since a variable
  438. * number of padding bytes precede the token and data.
  439. * + N(CX) (== 0..8) bytes of all-ones, before CSD or CID
  440. * + N(AC) (== 1..many) bytes of all-ones
  441. *
  442. * In this case we currently only have minimal speedups here:
  443. * when N(CR) == 1 we can avoid I/O in response_get().
  444. */
  445. if (cs_on && (mrq->data->flags & MMC_DATA_READ)) {
  446. cp += 2; /* min(N(CR)) + status */
  447. /* R1 */
  448. } else {
  449. cp += 10; /* max(N(CR)) + status + min(N(RC),N(WR)) */
  450. if (cmd->flags & MMC_RSP_SPI_S2) /* R2/R5 */
  451. cp++;
  452. else if (cmd->flags & MMC_RSP_SPI_B4) /* R3/R4/R7 */
  453. cp += 4;
  454. else if (cmd->flags & MMC_RSP_BUSY) /* R1B */
  455. cp = data->status + sizeof(data->status);
  456. /* else: R1 (most commands) */
  457. }
  458. dev_dbg(&host->spi->dev, " mmc_spi: CMD%d, resp %s\n",
  459. cmd->opcode, maptype(cmd));
  460. /* send command, leaving chipselect active */
  461. spi_message_init(&host->m);
  462. t = &host->t;
  463. memset(t, 0, sizeof(*t));
  464. t->tx_buf = t->rx_buf = data->status;
  465. t->tx_dma = t->rx_dma = host->data_dma;
  466. t->len = cp - data->status;
  467. t->cs_change = 1;
  468. spi_message_add_tail(t, &host->m);
  469. if (host->dma_dev) {
  470. host->m.is_dma_mapped = 1;
  471. dma_sync_single_for_device(host->dma_dev,
  472. host->data_dma, sizeof(*host->data),
  473. DMA_BIDIRECTIONAL);
  474. }
  475. status = spi_sync_locked(host->spi, &host->m);
  476. if (host->dma_dev)
  477. dma_sync_single_for_cpu(host->dma_dev,
  478. host->data_dma, sizeof(*host->data),
  479. DMA_BIDIRECTIONAL);
  480. if (status < 0) {
  481. dev_dbg(&host->spi->dev, " ... write returned %d\n", status);
  482. cmd->error = status;
  483. return status;
  484. }
  485. /* after no-data commands and STOP_TRANSMISSION, chipselect off */
  486. return mmc_spi_response_get(host, cmd, cs_on);
  487. }
  488. /* Build data message with up to four separate transfers. For TX, we
  489. * start by writing the data token. And in most cases, we finish with
  490. * a status transfer.
  491. *
  492. * We always provide TX data for data and CRC. The MMC/SD protocol
  493. * requires us to write ones; but Linux defaults to writing zeroes;
  494. * so we explicitly initialize it to all ones on RX paths.
  495. *
  496. * We also handle DMA mapping, so the underlying SPI controller does
  497. * not need to (re)do it for each message.
  498. */
  499. static void
  500. mmc_spi_setup_data_message(
  501. struct mmc_spi_host *host,
  502. int multiple,
  503. enum dma_data_direction direction)
  504. {
  505. struct spi_transfer *t;
  506. struct scratch *scratch = host->data;
  507. dma_addr_t dma = host->data_dma;
  508. spi_message_init(&host->m);
  509. if (dma)
  510. host->m.is_dma_mapped = 1;
  511. /* for reads, readblock() skips 0xff bytes before finding
  512. * the token; for writes, this transfer issues that token.
  513. */
  514. if (direction == DMA_TO_DEVICE) {
  515. t = &host->token;
  516. memset(t, 0, sizeof(*t));
  517. t->len = 1;
  518. if (multiple)
  519. scratch->data_token = SPI_TOKEN_MULTI_WRITE;
  520. else
  521. scratch->data_token = SPI_TOKEN_SINGLE;
  522. t->tx_buf = &scratch->data_token;
  523. if (dma)
  524. t->tx_dma = dma + offsetof(struct scratch, data_token);
  525. spi_message_add_tail(t, &host->m);
  526. }
  527. /* Body of transfer is buffer, then CRC ...
  528. * either TX-only, or RX with TX-ones.
  529. */
  530. t = &host->t;
  531. memset(t, 0, sizeof(*t));
  532. t->tx_buf = host->ones;
  533. t->tx_dma = host->ones_dma;
  534. /* length and actual buffer info are written later */
  535. spi_message_add_tail(t, &host->m);
  536. t = &host->crc;
  537. memset(t, 0, sizeof(*t));
  538. t->len = 2;
  539. if (direction == DMA_TO_DEVICE) {
  540. /* the actual CRC may get written later */
  541. t->tx_buf = &scratch->crc_val;
  542. if (dma)
  543. t->tx_dma = dma + offsetof(struct scratch, crc_val);
  544. } else {
  545. t->tx_buf = host->ones;
  546. t->tx_dma = host->ones_dma;
  547. t->rx_buf = &scratch->crc_val;
  548. if (dma)
  549. t->rx_dma = dma + offsetof(struct scratch, crc_val);
  550. }
  551. spi_message_add_tail(t, &host->m);
  552. /*
  553. * A single block read is followed by N(EC) [0+] all-ones bytes
  554. * before deselect ... don't bother.
  555. *
  556. * Multiblock reads are followed by N(AC) [1+] all-ones bytes before
  557. * the next block is read, or a STOP_TRANSMISSION is issued. We'll
  558. * collect that single byte, so readblock() doesn't need to.
  559. *
  560. * For a write, the one-byte data response follows immediately, then
  561. * come zero or more busy bytes, then N(WR) [1+] all-ones bytes.
  562. * Then single block reads may deselect, and multiblock ones issue
  563. * the next token (next data block, or STOP_TRAN). We can try to
  564. * minimize I/O ops by using a single read to collect end-of-busy.
  565. */
  566. if (multiple || direction == DMA_TO_DEVICE) {
  567. t = &host->early_status;
  568. memset(t, 0, sizeof(*t));
  569. t->len = (direction == DMA_TO_DEVICE)
  570. ? sizeof(scratch->status)
  571. : 1;
  572. t->tx_buf = host->ones;
  573. t->tx_dma = host->ones_dma;
  574. t->rx_buf = scratch->status;
  575. if (dma)
  576. t->rx_dma = dma + offsetof(struct scratch, status);
  577. t->cs_change = 1;
  578. spi_message_add_tail(t, &host->m);
  579. }
  580. }
  581. /*
  582. * Write one block:
  583. * - caller handled preceding N(WR) [1+] all-ones bytes
  584. * - data block
  585. * + token
  586. * + data bytes
  587. * + crc16
  588. * - an all-ones byte ... card writes a data-response byte
  589. * - followed by N(EC) [0+] all-ones bytes, card writes zero/'busy'
  590. *
  591. * Return negative errno, else success.
  592. */
  593. static int
  594. mmc_spi_writeblock(struct mmc_spi_host *host, struct spi_transfer *t,
  595. unsigned long timeout)
  596. {
  597. struct spi_device *spi = host->spi;
  598. int status, i;
  599. struct scratch *scratch = host->data;
  600. u32 pattern;
  601. if (host->mmc->use_spi_crc)
  602. scratch->crc_val = cpu_to_be16(
  603. crc_itu_t(0, t->tx_buf, t->len));
  604. if (host->dma_dev)
  605. dma_sync_single_for_device(host->dma_dev,
  606. host->data_dma, sizeof(*scratch),
  607. DMA_BIDIRECTIONAL);
  608. status = spi_sync_locked(spi, &host->m);
  609. if (status != 0) {
  610. dev_dbg(&spi->dev, "write error (%d)\n", status);
  611. return status;
  612. }
  613. if (host->dma_dev)
  614. dma_sync_single_for_cpu(host->dma_dev,
  615. host->data_dma, sizeof(*scratch),
  616. DMA_BIDIRECTIONAL);
  617. /*
  618. * Get the transmission data-response reply. It must follow
  619. * immediately after the data block we transferred. This reply
  620. * doesn't necessarily tell whether the write operation succeeded;
  621. * it just says if the transmission was ok and whether *earlier*
  622. * writes succeeded; see the standard.
  623. *
  624. * In practice, there are (even modern SDHC-)cards which are late
  625. * in sending the response, and miss the time frame by a few bits,
  626. * so we have to cope with this situation and check the response
  627. * bit-by-bit. Arggh!!!
  628. */
  629. pattern = scratch->status[0] << 24;
  630. pattern |= scratch->status[1] << 16;
  631. pattern |= scratch->status[2] << 8;
  632. pattern |= scratch->status[3];
  633. /* First 3 bit of pattern are undefined */
  634. pattern |= 0xE0000000;
  635. /* left-adjust to leading 0 bit */
  636. while (pattern & 0x80000000)
  637. pattern <<= 1;
  638. /* right-adjust for pattern matching. Code is in bit 4..0 now. */
  639. pattern >>= 27;
  640. switch (pattern) {
  641. case SPI_RESPONSE_ACCEPTED:
  642. status = 0;
  643. break;
  644. case SPI_RESPONSE_CRC_ERR:
  645. /* host shall then issue MMC_STOP_TRANSMISSION */
  646. status = -EILSEQ;
  647. break;
  648. case SPI_RESPONSE_WRITE_ERR:
  649. /* host shall then issue MMC_STOP_TRANSMISSION,
  650. * and should MMC_SEND_STATUS to sort it out
  651. */
  652. status = -EIO;
  653. break;
  654. default:
  655. status = -EPROTO;
  656. break;
  657. }
  658. if (status != 0) {
  659. dev_dbg(&spi->dev, "write error %02x (%d)\n",
  660. scratch->status[0], status);
  661. return status;
  662. }
  663. t->tx_buf += t->len;
  664. if (host->dma_dev)
  665. t->tx_dma += t->len;
  666. /* Return when not busy. If we didn't collect that status yet,
  667. * we'll need some more I/O.
  668. */
  669. for (i = 4; i < sizeof(scratch->status); i++) {
  670. /* card is non-busy if the most recent bit is 1 */
  671. if (scratch->status[i] & 0x01)
  672. return 0;
  673. }
  674. return mmc_spi_wait_unbusy(host, timeout);
  675. }
  676. /*
  677. * Read one block:
  678. * - skip leading all-ones bytes ... either
  679. * + N(AC) [1..f(clock,CSD)] usually, else
  680. * + N(CX) [0..8] when reading CSD or CID
  681. * - data block
  682. * + token ... if error token, no data or crc
  683. * + data bytes
  684. * + crc16
  685. *
  686. * After single block reads, we're done; N(EC) [0+] all-ones bytes follow
  687. * before dropping chipselect.
  688. *
  689. * For multiblock reads, caller either reads the next block or issues a
  690. * STOP_TRANSMISSION command.
  691. */
  692. static int
  693. mmc_spi_readblock(struct mmc_spi_host *host, struct spi_transfer *t,
  694. unsigned long timeout)
  695. {
  696. struct spi_device *spi = host->spi;
  697. int status;
  698. struct scratch *scratch = host->data;
  699. unsigned int bitshift;
  700. u8 leftover;
  701. /* At least one SD card sends an all-zeroes byte when N(CX)
  702. * applies, before the all-ones bytes ... just cope with that.
  703. */
  704. status = mmc_spi_readbytes(host, 1);
  705. if (status < 0)
  706. return status;
  707. status = scratch->status[0];
  708. if (status == 0xff || status == 0)
  709. status = mmc_spi_readtoken(host, timeout);
  710. if (status < 0) {
  711. dev_dbg(&spi->dev, "read error %02x (%d)\n", status, status);
  712. return status;
  713. }
  714. /* The token may be bit-shifted...
  715. * the first 0-bit precedes the data stream.
  716. */
  717. bitshift = 7;
  718. while (status & 0x80) {
  719. status <<= 1;
  720. bitshift--;
  721. }
  722. leftover = status << 1;
  723. if (host->dma_dev) {
  724. dma_sync_single_for_device(host->dma_dev,
  725. host->data_dma, sizeof(*scratch),
  726. DMA_BIDIRECTIONAL);
  727. dma_sync_single_for_device(host->dma_dev,
  728. t->rx_dma, t->len,
  729. DMA_FROM_DEVICE);
  730. }
  731. status = spi_sync_locked(spi, &host->m);
  732. if (host->dma_dev) {
  733. dma_sync_single_for_cpu(host->dma_dev,
  734. host->data_dma, sizeof(*scratch),
  735. DMA_BIDIRECTIONAL);
  736. dma_sync_single_for_cpu(host->dma_dev,
  737. t->rx_dma, t->len,
  738. DMA_FROM_DEVICE);
  739. }
  740. if (bitshift) {
  741. /* Walk through the data and the crc and do
  742. * all the magic to get byte-aligned data.
  743. */
  744. u8 *cp = t->rx_buf;
  745. unsigned int len;
  746. unsigned int bitright = 8 - bitshift;
  747. u8 temp;
  748. for (len = t->len; len; len--) {
  749. temp = *cp;
  750. *cp++ = leftover | (temp >> bitshift);
  751. leftover = temp << bitright;
  752. }
  753. cp = (u8 *) &scratch->crc_val;
  754. temp = *cp;
  755. *cp++ = leftover | (temp >> bitshift);
  756. leftover = temp << bitright;
  757. temp = *cp;
  758. *cp = leftover | (temp >> bitshift);
  759. }
  760. if (host->mmc->use_spi_crc) {
  761. u16 crc = crc_itu_t(0, t->rx_buf, t->len);
  762. be16_to_cpus(&scratch->crc_val);
  763. if (scratch->crc_val != crc) {
  764. dev_dbg(&spi->dev, "read - crc error: crc_val=0x%04x, "
  765. "computed=0x%04x len=%d\n",
  766. scratch->crc_val, crc, t->len);
  767. return -EILSEQ;
  768. }
  769. }
  770. t->rx_buf += t->len;
  771. if (host->dma_dev)
  772. t->rx_dma += t->len;
  773. return 0;
  774. }
  775. /*
  776. * An MMC/SD data stage includes one or more blocks, optional CRCs,
  777. * and inline handshaking. That handhaking makes it unlike most
  778. * other SPI protocol stacks.
  779. */
  780. static void
  781. mmc_spi_data_do(struct mmc_spi_host *host, struct mmc_command *cmd,
  782. struct mmc_data *data, u32 blk_size)
  783. {
  784. struct spi_device *spi = host->spi;
  785. struct device *dma_dev = host->dma_dev;
  786. struct spi_transfer *t;
  787. enum dma_data_direction direction;
  788. struct scatterlist *sg;
  789. unsigned n_sg;
  790. int multiple = (data->blocks > 1);
  791. u32 clock_rate;
  792. unsigned long timeout;
  793. if (data->flags & MMC_DATA_READ)
  794. direction = DMA_FROM_DEVICE;
  795. else
  796. direction = DMA_TO_DEVICE;
  797. mmc_spi_setup_data_message(host, multiple, direction);
  798. t = &host->t;
  799. if (t->speed_hz)
  800. clock_rate = t->speed_hz;
  801. else
  802. clock_rate = spi->max_speed_hz;
  803. timeout = data->timeout_ns +
  804. data->timeout_clks * 1000000 / clock_rate;
  805. timeout = usecs_to_jiffies((unsigned int)(timeout / 1000)) + 1;
  806. /* Handle scatterlist segments one at a time, with synch for
  807. * each 512-byte block
  808. */
  809. for (sg = data->sg, n_sg = data->sg_len; n_sg; n_sg--, sg++) {
  810. int status = 0;
  811. dma_addr_t dma_addr = 0;
  812. void *kmap_addr;
  813. unsigned length = sg->length;
  814. enum dma_data_direction dir = direction;
  815. /* set up dma mapping for controller drivers that might
  816. * use DMA ... though they may fall back to PIO
  817. */
  818. if (dma_dev) {
  819. /* never invalidate whole *shared* pages ... */
  820. if ((sg->offset != 0 || length != PAGE_SIZE)
  821. && dir == DMA_FROM_DEVICE)
  822. dir = DMA_BIDIRECTIONAL;
  823. dma_addr = dma_map_page(dma_dev, sg_page(sg), 0,
  824. PAGE_SIZE, dir);
  825. if (direction == DMA_TO_DEVICE)
  826. t->tx_dma = dma_addr + sg->offset;
  827. else
  828. t->rx_dma = dma_addr + sg->offset;
  829. }
  830. /* allow pio too; we don't allow highmem */
  831. kmap_addr = kmap(sg_page(sg));
  832. if (direction == DMA_TO_DEVICE)
  833. t->tx_buf = kmap_addr + sg->offset;
  834. else
  835. t->rx_buf = kmap_addr + sg->offset;
  836. /* transfer each block, and update request status */
  837. while (length) {
  838. t->len = min(length, blk_size);
  839. dev_dbg(&host->spi->dev,
  840. " mmc_spi: %s block, %d bytes\n",
  841. (direction == DMA_TO_DEVICE)
  842. ? "write"
  843. : "read",
  844. t->len);
  845. if (direction == DMA_TO_DEVICE)
  846. status = mmc_spi_writeblock(host, t, timeout);
  847. else
  848. status = mmc_spi_readblock(host, t, timeout);
  849. if (status < 0)
  850. break;
  851. data->bytes_xfered += t->len;
  852. length -= t->len;
  853. if (!multiple)
  854. break;
  855. }
  856. /* discard mappings */
  857. if (direction == DMA_FROM_DEVICE)
  858. flush_kernel_dcache_page(sg_page(sg));
  859. kunmap(sg_page(sg));
  860. if (dma_dev)
  861. dma_unmap_page(dma_dev, dma_addr, PAGE_SIZE, dir);
  862. if (status < 0) {
  863. data->error = status;
  864. dev_dbg(&spi->dev, "%s status %d\n",
  865. (direction == DMA_TO_DEVICE)
  866. ? "write" : "read",
  867. status);
  868. break;
  869. }
  870. }
  871. /* NOTE some docs describe an MMC-only SET_BLOCK_COUNT (CMD23) that
  872. * can be issued before multiblock writes. Unlike its more widely
  873. * documented analogue for SD cards (SET_WR_BLK_ERASE_COUNT, ACMD23),
  874. * that can affect the STOP_TRAN logic. Complete (and current)
  875. * MMC specs should sort that out before Linux starts using CMD23.
  876. */
  877. if (direction == DMA_TO_DEVICE && multiple) {
  878. struct scratch *scratch = host->data;
  879. int tmp;
  880. const unsigned statlen = sizeof(scratch->status);
  881. dev_dbg(&spi->dev, " mmc_spi: STOP_TRAN\n");
  882. /* Tweak the per-block message we set up earlier by morphing
  883. * it to hold single buffer with the token followed by some
  884. * all-ones bytes ... skip N(BR) (0..1), scan the rest for
  885. * "not busy any longer" status, and leave chip selected.
  886. */
  887. INIT_LIST_HEAD(&host->m.transfers);
  888. list_add(&host->early_status.transfer_list,
  889. &host->m.transfers);
  890. memset(scratch->status, 0xff, statlen);
  891. scratch->status[0] = SPI_TOKEN_STOP_TRAN;
  892. host->early_status.tx_buf = host->early_status.rx_buf;
  893. host->early_status.tx_dma = host->early_status.rx_dma;
  894. host->early_status.len = statlen;
  895. if (host->dma_dev)
  896. dma_sync_single_for_device(host->dma_dev,
  897. host->data_dma, sizeof(*scratch),
  898. DMA_BIDIRECTIONAL);
  899. tmp = spi_sync_locked(spi, &host->m);
  900. if (host->dma_dev)
  901. dma_sync_single_for_cpu(host->dma_dev,
  902. host->data_dma, sizeof(*scratch),
  903. DMA_BIDIRECTIONAL);
  904. if (tmp < 0) {
  905. if (!data->error)
  906. data->error = tmp;
  907. return;
  908. }
  909. /* Ideally we collected "not busy" status with one I/O,
  910. * avoiding wasteful byte-at-a-time scanning... but more
  911. * I/O is often needed.
  912. */
  913. for (tmp = 2; tmp < statlen; tmp++) {
  914. if (scratch->status[tmp] != 0)
  915. return;
  916. }
  917. tmp = mmc_spi_wait_unbusy(host, timeout);
  918. if (tmp < 0 && !data->error)
  919. data->error = tmp;
  920. }
  921. }
  922. /****************************************************************************/
  923. /*
  924. * MMC driver implementation -- the interface to the MMC stack
  925. */
  926. static void mmc_spi_request(struct mmc_host *mmc, struct mmc_request *mrq)
  927. {
  928. struct mmc_spi_host *host = mmc_priv(mmc);
  929. int status = -EINVAL;
  930. int crc_retry = 5;
  931. struct mmc_command stop;
  932. #ifdef DEBUG
  933. /* MMC core and layered drivers *MUST* issue SPI-aware commands */
  934. {
  935. struct mmc_command *cmd;
  936. int invalid = 0;
  937. cmd = mrq->cmd;
  938. if (!mmc_spi_resp_type(cmd)) {
  939. dev_dbg(&host->spi->dev, "bogus command\n");
  940. cmd->error = -EINVAL;
  941. invalid = 1;
  942. }
  943. cmd = mrq->stop;
  944. if (cmd && !mmc_spi_resp_type(cmd)) {
  945. dev_dbg(&host->spi->dev, "bogus STOP command\n");
  946. cmd->error = -EINVAL;
  947. invalid = 1;
  948. }
  949. if (invalid) {
  950. dump_stack();
  951. mmc_request_done(host->mmc, mrq);
  952. return;
  953. }
  954. }
  955. #endif
  956. /* request exclusive bus access */
  957. spi_bus_lock(host->spi->master);
  958. crc_recover:
  959. /* issue command; then optionally data and stop */
  960. status = mmc_spi_command_send(host, mrq, mrq->cmd, mrq->data != NULL);
  961. if (status == 0 && mrq->data) {
  962. mmc_spi_data_do(host, mrq->cmd, mrq->data, mrq->data->blksz);
  963. /*
  964. * The SPI bus is not always reliable for large data transfers.
  965. * If an occasional crc error is reported by the SD device with
  966. * data read/write over SPI, it may be recovered by repeating
  967. * the last SD command again. The retry count is set to 5 to
  968. * ensure the driver passes stress tests.
  969. */
  970. if (mrq->data->error == -EILSEQ && crc_retry) {
  971. stop.opcode = MMC_STOP_TRANSMISSION;
  972. stop.arg = 0;
  973. stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  974. status = mmc_spi_command_send(host, mrq, &stop, 0);
  975. crc_retry--;
  976. mrq->data->error = 0;
  977. goto crc_recover;
  978. }
  979. if (mrq->stop)
  980. status = mmc_spi_command_send(host, mrq, mrq->stop, 0);
  981. else
  982. mmc_cs_off(host);
  983. }
  984. /* release the bus */
  985. spi_bus_unlock(host->spi->master);
  986. mmc_request_done(host->mmc, mrq);
  987. }
  988. /* See Section 6.4.1, in SD "Simplified Physical Layer Specification 2.0"
  989. *
  990. * NOTE that here we can't know that the card has just been powered up;
  991. * not all MMC/SD sockets support power switching.
  992. *
  993. * FIXME when the card is still in SPI mode, e.g. from a previous kernel,
  994. * this doesn't seem to do the right thing at all...
  995. */
  996. static void mmc_spi_initsequence(struct mmc_spi_host *host)
  997. {
  998. /* Try to be very sure any previous command has completed;
  999. * wait till not-busy, skip debris from any old commands.
  1000. */
  1001. mmc_spi_wait_unbusy(host, r1b_timeout);
  1002. mmc_spi_readbytes(host, 10);
  1003. /*
  1004. * Do a burst with chipselect active-high. We need to do this to
  1005. * meet the requirement of 74 clock cycles with both chipselect
  1006. * and CMD (MOSI) high before CMD0 ... after the card has been
  1007. * powered up to Vdd(min), and so is ready to take commands.
  1008. *
  1009. * Some cards are particularly needy of this (e.g. Viking "SD256")
  1010. * while most others don't seem to care.
  1011. *
  1012. * Note that this is one of the places MMC/SD plays games with the
  1013. * SPI protocol. Another is that when chipselect is released while
  1014. * the card returns BUSY status, the clock must issue several cycles
  1015. * with chipselect high before the card will stop driving its output.
  1016. */
  1017. host->spi->mode |= SPI_CS_HIGH;
  1018. if (spi_setup(host->spi) != 0) {
  1019. /* Just warn; most cards work without it. */
  1020. dev_warn(&host->spi->dev,
  1021. "can't change chip-select polarity\n");
  1022. host->spi->mode &= ~SPI_CS_HIGH;
  1023. } else {
  1024. mmc_spi_readbytes(host, 18);
  1025. host->spi->mode &= ~SPI_CS_HIGH;
  1026. if (spi_setup(host->spi) != 0) {
  1027. /* Wot, we can't get the same setup we had before? */
  1028. dev_err(&host->spi->dev,
  1029. "can't restore chip-select polarity\n");
  1030. }
  1031. }
  1032. }
  1033. static char *mmc_powerstring(u8 power_mode)
  1034. {
  1035. switch (power_mode) {
  1036. case MMC_POWER_OFF: return "off";
  1037. case MMC_POWER_UP: return "up";
  1038. case MMC_POWER_ON: return "on";
  1039. }
  1040. return "?";
  1041. }
  1042. static void mmc_spi_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1043. {
  1044. struct mmc_spi_host *host = mmc_priv(mmc);
  1045. if (host->power_mode != ios->power_mode) {
  1046. int canpower;
  1047. canpower = host->pdata && host->pdata->setpower;
  1048. dev_dbg(&host->spi->dev, "mmc_spi: power %s (%d)%s\n",
  1049. mmc_powerstring(ios->power_mode),
  1050. ios->vdd,
  1051. canpower ? ", can switch" : "");
  1052. /* switch power on/off if possible, accounting for
  1053. * max 250msec powerup time if needed.
  1054. */
  1055. if (canpower) {
  1056. switch (ios->power_mode) {
  1057. case MMC_POWER_OFF:
  1058. case MMC_POWER_UP:
  1059. host->pdata->setpower(&host->spi->dev,
  1060. ios->vdd);
  1061. if (ios->power_mode == MMC_POWER_UP)
  1062. msleep(host->powerup_msecs);
  1063. }
  1064. }
  1065. /* See 6.4.1 in the simplified SD card physical spec 2.0 */
  1066. if (ios->power_mode == MMC_POWER_ON)
  1067. mmc_spi_initsequence(host);
  1068. /* If powering down, ground all card inputs to avoid power
  1069. * delivery from data lines! On a shared SPI bus, this
  1070. * will probably be temporary; 6.4.2 of the simplified SD
  1071. * spec says this must last at least 1msec.
  1072. *
  1073. * - Clock low means CPOL 0, e.g. mode 0
  1074. * - MOSI low comes from writing zero
  1075. * - Chipselect is usually active low...
  1076. */
  1077. if (canpower && ios->power_mode == MMC_POWER_OFF) {
  1078. int mres;
  1079. u8 nullbyte = 0;
  1080. host->spi->mode &= ~(SPI_CPOL|SPI_CPHA);
  1081. mres = spi_setup(host->spi);
  1082. if (mres < 0)
  1083. dev_dbg(&host->spi->dev,
  1084. "switch to SPI mode 0 failed\n");
  1085. if (spi_write(host->spi, &nullbyte, 1) < 0)
  1086. dev_dbg(&host->spi->dev,
  1087. "put spi signals to low failed\n");
  1088. /*
  1089. * Now clock should be low due to spi mode 0;
  1090. * MOSI should be low because of written 0x00;
  1091. * chipselect should be low (it is active low)
  1092. * power supply is off, so now MMC is off too!
  1093. *
  1094. * FIXME no, chipselect can be high since the
  1095. * device is inactive and SPI_CS_HIGH is clear...
  1096. */
  1097. msleep(10);
  1098. if (mres == 0) {
  1099. host->spi->mode |= (SPI_CPOL|SPI_CPHA);
  1100. mres = spi_setup(host->spi);
  1101. if (mres < 0)
  1102. dev_dbg(&host->spi->dev,
  1103. "switch back to SPI mode 3"
  1104. " failed\n");
  1105. }
  1106. }
  1107. host->power_mode = ios->power_mode;
  1108. }
  1109. if (host->spi->max_speed_hz != ios->clock && ios->clock != 0) {
  1110. int status;
  1111. host->spi->max_speed_hz = ios->clock;
  1112. status = spi_setup(host->spi);
  1113. dev_dbg(&host->spi->dev,
  1114. "mmc_spi: clock to %d Hz, %d\n",
  1115. host->spi->max_speed_hz, status);
  1116. }
  1117. }
  1118. static int mmc_spi_get_ro(struct mmc_host *mmc)
  1119. {
  1120. struct mmc_spi_host *host = mmc_priv(mmc);
  1121. if (host->pdata && host->pdata->get_ro)
  1122. return !!host->pdata->get_ro(mmc->parent);
  1123. /*
  1124. * Board doesn't support read only detection; let the mmc core
  1125. * decide what to do.
  1126. */
  1127. return -ENOSYS;
  1128. }
  1129. static int mmc_spi_get_cd(struct mmc_host *mmc)
  1130. {
  1131. struct mmc_spi_host *host = mmc_priv(mmc);
  1132. if (host->pdata && host->pdata->get_cd)
  1133. return !!host->pdata->get_cd(mmc->parent);
  1134. return -ENOSYS;
  1135. }
  1136. static const struct mmc_host_ops mmc_spi_ops = {
  1137. .request = mmc_spi_request,
  1138. .set_ios = mmc_spi_set_ios,
  1139. .get_ro = mmc_spi_get_ro,
  1140. .get_cd = mmc_spi_get_cd,
  1141. };
  1142. /****************************************************************************/
  1143. /*
  1144. * SPI driver implementation
  1145. */
  1146. static irqreturn_t
  1147. mmc_spi_detect_irq(int irq, void *mmc)
  1148. {
  1149. struct mmc_spi_host *host = mmc_priv(mmc);
  1150. u16 delay_msec = max(host->pdata->detect_delay, (u16)100);
  1151. mmc_detect_change(mmc, msecs_to_jiffies(delay_msec));
  1152. return IRQ_HANDLED;
  1153. }
  1154. static int mmc_spi_probe(struct spi_device *spi)
  1155. {
  1156. void *ones;
  1157. struct mmc_host *mmc;
  1158. struct mmc_spi_host *host;
  1159. int status;
  1160. /* We rely on full duplex transfers, mostly to reduce
  1161. * per-transfer overheads (by making fewer transfers).
  1162. */
  1163. if (spi->master->flags & SPI_MASTER_HALF_DUPLEX)
  1164. return -EINVAL;
  1165. /* MMC and SD specs only seem to care that sampling is on the
  1166. * rising edge ... meaning SPI modes 0 or 3. So either SPI mode
  1167. * should be legit. We'll use mode 0 since the steady state is 0,
  1168. * which is appropriate for hotplugging, unless the platform data
  1169. * specify mode 3 (if hardware is not compatible to mode 0).
  1170. */
  1171. if (spi->mode != SPI_MODE_3)
  1172. spi->mode = SPI_MODE_0;
  1173. spi->bits_per_word = 8;
  1174. status = spi_setup(spi);
  1175. if (status < 0) {
  1176. dev_dbg(&spi->dev, "needs SPI mode %02x, %d KHz; %d\n",
  1177. spi->mode, spi->max_speed_hz / 1000,
  1178. status);
  1179. return status;
  1180. }
  1181. /* We need a supply of ones to transmit. This is the only time
  1182. * the CPU touches these, so cache coherency isn't a concern.
  1183. *
  1184. * NOTE if many systems use more than one MMC-over-SPI connector
  1185. * it'd save some memory to share this. That's evidently rare.
  1186. */
  1187. status = -ENOMEM;
  1188. ones = kmalloc(MMC_SPI_BLOCKSIZE, GFP_KERNEL);
  1189. if (!ones)
  1190. goto nomem;
  1191. memset(ones, 0xff, MMC_SPI_BLOCKSIZE);
  1192. mmc = mmc_alloc_host(sizeof(*host), &spi->dev);
  1193. if (!mmc)
  1194. goto nomem;
  1195. mmc->ops = &mmc_spi_ops;
  1196. mmc->max_blk_size = MMC_SPI_BLOCKSIZE;
  1197. mmc->max_segs = MMC_SPI_BLOCKSATONCE;
  1198. mmc->max_req_size = MMC_SPI_BLOCKSATONCE * MMC_SPI_BLOCKSIZE;
  1199. mmc->max_blk_count = MMC_SPI_BLOCKSATONCE;
  1200. mmc->caps = MMC_CAP_SPI;
  1201. /* SPI doesn't need the lowspeed device identification thing for
  1202. * MMC or SD cards, since it never comes up in open drain mode.
  1203. * That's good; some SPI masters can't handle very low speeds!
  1204. *
  1205. * However, low speed SDIO cards need not handle over 400 KHz;
  1206. * that's the only reason not to use a few MHz for f_min (until
  1207. * the upper layer reads the target frequency from the CSD).
  1208. */
  1209. mmc->f_min = 400000;
  1210. mmc->f_max = spi->max_speed_hz;
  1211. host = mmc_priv(mmc);
  1212. host->mmc = mmc;
  1213. host->spi = spi;
  1214. host->ones = ones;
  1215. /* Platform data is used to hook up things like card sensing
  1216. * and power switching gpios.
  1217. */
  1218. host->pdata = mmc_spi_get_pdata(spi);
  1219. if (host->pdata)
  1220. mmc->ocr_avail = host->pdata->ocr_mask;
  1221. if (!mmc->ocr_avail) {
  1222. dev_warn(&spi->dev, "ASSUMING 3.2-3.4 V slot power\n");
  1223. mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34;
  1224. }
  1225. if (host->pdata && host->pdata->setpower) {
  1226. host->powerup_msecs = host->pdata->powerup_msecs;
  1227. if (!host->powerup_msecs || host->powerup_msecs > 250)
  1228. host->powerup_msecs = 250;
  1229. }
  1230. dev_set_drvdata(&spi->dev, mmc);
  1231. /* preallocate dma buffers */
  1232. host->data = kmalloc(sizeof(*host->data), GFP_KERNEL);
  1233. if (!host->data)
  1234. goto fail_nobuf1;
  1235. if (spi->master->dev.parent->dma_mask) {
  1236. struct device *dev = spi->master->dev.parent;
  1237. host->dma_dev = dev;
  1238. host->ones_dma = dma_map_single(dev, ones,
  1239. MMC_SPI_BLOCKSIZE, DMA_TO_DEVICE);
  1240. host->data_dma = dma_map_single(dev, host->data,
  1241. sizeof(*host->data), DMA_BIDIRECTIONAL);
  1242. /* REVISIT in theory those map operations can fail... */
  1243. dma_sync_single_for_cpu(host->dma_dev,
  1244. host->data_dma, sizeof(*host->data),
  1245. DMA_BIDIRECTIONAL);
  1246. }
  1247. /* setup message for status/busy readback */
  1248. spi_message_init(&host->readback);
  1249. host->readback.is_dma_mapped = (host->dma_dev != NULL);
  1250. spi_message_add_tail(&host->status, &host->readback);
  1251. host->status.tx_buf = host->ones;
  1252. host->status.tx_dma = host->ones_dma;
  1253. host->status.rx_buf = &host->data->status;
  1254. host->status.rx_dma = host->data_dma + offsetof(struct scratch, status);
  1255. host->status.cs_change = 1;
  1256. /* register card detect irq */
  1257. if (host->pdata && host->pdata->init) {
  1258. status = host->pdata->init(&spi->dev, mmc_spi_detect_irq, mmc);
  1259. if (status != 0)
  1260. goto fail_glue_init;
  1261. }
  1262. /* pass platform capabilities, if any */
  1263. if (host->pdata)
  1264. mmc->caps |= host->pdata->caps;
  1265. status = mmc_add_host(mmc);
  1266. if (status != 0)
  1267. goto fail_add_host;
  1268. dev_info(&spi->dev, "SD/MMC host %s%s%s%s%s\n",
  1269. dev_name(&mmc->class_dev),
  1270. host->dma_dev ? "" : ", no DMA",
  1271. (host->pdata && host->pdata->get_ro)
  1272. ? "" : ", no WP",
  1273. (host->pdata && host->pdata->setpower)
  1274. ? "" : ", no poweroff",
  1275. (mmc->caps & MMC_CAP_NEEDS_POLL)
  1276. ? ", cd polling" : "");
  1277. return 0;
  1278. fail_add_host:
  1279. mmc_remove_host (mmc);
  1280. fail_glue_init:
  1281. if (host->dma_dev)
  1282. dma_unmap_single(host->dma_dev, host->data_dma,
  1283. sizeof(*host->data), DMA_BIDIRECTIONAL);
  1284. kfree(host->data);
  1285. fail_nobuf1:
  1286. mmc_free_host(mmc);
  1287. mmc_spi_put_pdata(spi);
  1288. dev_set_drvdata(&spi->dev, NULL);
  1289. nomem:
  1290. kfree(ones);
  1291. return status;
  1292. }
  1293. static int __devexit mmc_spi_remove(struct spi_device *spi)
  1294. {
  1295. struct mmc_host *mmc = dev_get_drvdata(&spi->dev);
  1296. struct mmc_spi_host *host;
  1297. if (mmc) {
  1298. host = mmc_priv(mmc);
  1299. /* prevent new mmc_detect_change() calls */
  1300. if (host->pdata && host->pdata->exit)
  1301. host->pdata->exit(&spi->dev, mmc);
  1302. mmc_remove_host(mmc);
  1303. if (host->dma_dev) {
  1304. dma_unmap_single(host->dma_dev, host->ones_dma,
  1305. MMC_SPI_BLOCKSIZE, DMA_TO_DEVICE);
  1306. dma_unmap_single(host->dma_dev, host->data_dma,
  1307. sizeof(*host->data), DMA_BIDIRECTIONAL);
  1308. }
  1309. kfree(host->data);
  1310. kfree(host->ones);
  1311. spi->max_speed_hz = mmc->f_max;
  1312. mmc_free_host(mmc);
  1313. mmc_spi_put_pdata(spi);
  1314. dev_set_drvdata(&spi->dev, NULL);
  1315. }
  1316. return 0;
  1317. }
  1318. static struct of_device_id mmc_spi_of_match_table[] __devinitdata = {
  1319. { .compatible = "mmc-spi-slot", },
  1320. {},
  1321. };
  1322. static struct spi_driver mmc_spi_driver = {
  1323. .driver = {
  1324. .name = "mmc_spi",
  1325. .bus = &spi_bus_type,
  1326. .owner = THIS_MODULE,
  1327. .of_match_table = mmc_spi_of_match_table,
  1328. },
  1329. .probe = mmc_spi_probe,
  1330. .remove = __devexit_p(mmc_spi_remove),
  1331. };
  1332. static int __init mmc_spi_init(void)
  1333. {
  1334. return spi_register_driver(&mmc_spi_driver);
  1335. }
  1336. module_init(mmc_spi_init);
  1337. static void __exit mmc_spi_exit(void)
  1338. {
  1339. spi_unregister_driver(&mmc_spi_driver);
  1340. }
  1341. module_exit(mmc_spi_exit);
  1342. MODULE_AUTHOR("Mike Lavender, David Brownell, "
  1343. "Hans-Peter Nilsson, Jan Nikitenko");
  1344. MODULE_DESCRIPTION("SPI SD/MMC host driver");
  1345. MODULE_LICENSE("GPL");
  1346. MODULE_ALIAS("spi:mmc_spi");