mmc_spi.c 38 KB

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