mmc_spi.c 38 KB

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