mmc_spi.c 40 KB

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