mmc_spi.c 42 KB

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