mmc_spi.c 39 KB

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