mmc_spi.c 42 KB

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