mmc_spi.c 42 KB

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