mmc_spi.c 39 KB

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