mmc_spi.c 42 KB

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