mmc_spi.c 41 KB

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