mmc_spi.c 38 KB

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