mmc_spi.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Access SD/MMC cards through SPI master controllers
  4. *
  5. * (C) Copyright 2005, Intec Automation,
  6. * Mike Lavender (mike@steroidmicros)
  7. * (C) Copyright 2006-2007, David Brownell
  8. * (C) Copyright 2007, Axis Communications,
  9. * Hans-Peter Nilsson (hp@axis.com)
  10. * (C) Copyright 2007, ATRON electronic GmbH,
  11. * Jan Nikitenko <jan.nikitenko@gmail.com>
  12. */
  13. #include <linux/sched.h>
  14. #include <linux/delay.h>
  15. #include <linux/slab.h>
  16. #include <linux/module.h>
  17. #include <linux/bio.h>
  18. #include <linux/crc7.h>
  19. #include <linux/crc-itu-t.h>
  20. #include <linux/scatterlist.h>
  21. #include <linux/mmc/host.h>
  22. #include <linux/mmc/mmc.h> /* for R1_SPI_* bit values */
  23. #include <linux/mmc/slot-gpio.h>
  24. #include <linux/spi/spi.h>
  25. #include <linux/spi/mmc_spi.h>
  26. #include <linux/unaligned.h>
  27. /* NOTES:
  28. *
  29. * - For now, we won't try to interoperate with a real mmc/sd/sdio
  30. * controller, although some of them do have hardware support for
  31. * SPI protocol. The main reason for such configs would be mmc-ish
  32. * cards like DataFlash, which don't support that "native" protocol.
  33. *
  34. * We don't have a "DataFlash/MMC/SD/SDIO card slot" abstraction to
  35. * switch between driver stacks, and in any case if "native" mode
  36. * is available, it will be faster and hence preferable.
  37. *
  38. * - MMC depends on a different chipselect management policy than the
  39. * SPI interface currently supports for shared bus segments: it needs
  40. * to issue multiple spi_message requests with the chipselect active,
  41. * using the results of one message to decide the next one to issue.
  42. *
  43. * Pending updates to the programming interface, this driver expects
  44. * that it not share the bus with other drivers (precluding conflicts).
  45. *
  46. * - We tell the controller to keep the chipselect active from the
  47. * beginning of an mmc_host_ops.request until the end. So beware
  48. * of SPI controller drivers that mis-handle the cs_change flag!
  49. *
  50. * However, many cards seem OK with chipselect flapping up/down
  51. * during that time ... at least on unshared bus segments.
  52. */
  53. /*
  54. * Local protocol constants, internal to data block protocols.
  55. */
  56. /* Response tokens used to ack each block written: */
  57. #define SPI_MMC_RESPONSE_CODE(x) ((x) & 0x1f)
  58. #define SPI_RESPONSE_ACCEPTED ((2 << 1)|1)
  59. #define SPI_RESPONSE_CRC_ERR ((5 << 1)|1)
  60. #define SPI_RESPONSE_WRITE_ERR ((6 << 1)|1)
  61. /* Read and write blocks start with these tokens and end with crc;
  62. * on error, read tokens act like a subset of R2_SPI_* values.
  63. */
  64. #define SPI_TOKEN_SINGLE 0xfe /* single block r/w, multiblock read */
  65. #define SPI_TOKEN_MULTI_WRITE 0xfc /* multiblock write */
  66. #define SPI_TOKEN_STOP_TRAN 0xfd /* terminate multiblock write */
  67. #define MMC_SPI_BLOCKSIZE 512
  68. #define MMC_SPI_R1B_TIMEOUT_MS 3000
  69. #define MMC_SPI_INIT_TIMEOUT_MS 3000
  70. /* One of the critical speed parameters is the amount of data which may
  71. * be transferred in one command. If this value is too low, the SD card
  72. * controller has to do multiple partial block writes (argggh!). With
  73. * today (2008) SD cards there is little speed gain if we transfer more
  74. * than 64 KBytes at a time. So use this value until there is any indication
  75. * that we should do more here.
  76. */
  77. #define MMC_SPI_BLOCKSATONCE 128
  78. /****************************************************************************/
  79. /*
  80. * Local Data Structures
  81. */
  82. /* "scratch" is per-{command,block} data exchanged with the card */
  83. struct scratch {
  84. u8 status[29];
  85. u8 data_token;
  86. __be16 crc_val;
  87. };
  88. struct mmc_spi_host {
  89. struct mmc_host *mmc;
  90. struct spi_device *spi;
  91. unsigned char power_mode;
  92. u16 powerup_msecs;
  93. struct mmc_spi_platform_data *pdata;
  94. /* for bulk data transfers */
  95. struct spi_transfer token, t, crc, early_status;
  96. struct spi_message m;
  97. /* for status readback */
  98. struct spi_transfer status;
  99. struct spi_message readback;
  100. /* buffer used for commands and for message "overhead" */
  101. struct scratch *data;
  102. /* Specs say to write ones most of the time, even when the card
  103. * has no need to read its input data; and many cards won't care.
  104. * This is our source of those ones.
  105. */
  106. void *ones;
  107. };
  108. /****************************************************************************/
  109. /*
  110. * MMC-over-SPI protocol glue, used by the MMC stack interface
  111. */
  112. static inline int mmc_cs_off(struct mmc_spi_host *host)
  113. {
  114. /* chipselect will always be inactive after setup() */
  115. return spi_setup(host->spi);
  116. }
  117. static int mmc_spi_readbytes(struct mmc_spi_host *host, unsigned int len)
  118. {
  119. if (len > sizeof(*host->data)) {
  120. WARN_ON(1);
  121. return -EIO;
  122. }
  123. host->status.len = len;
  124. return spi_sync_locked(host->spi, &host->readback);
  125. }
  126. static int mmc_spi_skip(struct mmc_spi_host *host, unsigned long timeout,
  127. unsigned n, u8 byte)
  128. {
  129. u8 *cp = host->data->status;
  130. unsigned long start = jiffies;
  131. do {
  132. int status;
  133. unsigned i;
  134. status = mmc_spi_readbytes(host, n);
  135. if (status < 0)
  136. return status;
  137. for (i = 0; i < n; i++) {
  138. if (cp[i] != byte)
  139. return cp[i];
  140. }
  141. /* If we need long timeouts, we may release the CPU */
  142. cond_resched();
  143. } while (time_is_after_jiffies(start + timeout));
  144. return -ETIMEDOUT;
  145. }
  146. static inline int
  147. mmc_spi_wait_unbusy(struct mmc_spi_host *host, unsigned long timeout)
  148. {
  149. return mmc_spi_skip(host, timeout, sizeof(host->data->status), 0);
  150. }
  151. static int mmc_spi_readtoken(struct mmc_spi_host *host, unsigned long timeout)
  152. {
  153. return mmc_spi_skip(host, timeout, 1, 0xff);
  154. }
  155. /*
  156. * Note that for SPI, cmd->resp[0] is not the same data as "native" protocol
  157. * hosts return! The low byte holds R1_SPI bits. The next byte may hold
  158. * R2_SPI bits ... for SEND_STATUS, or after data read errors.
  159. *
  160. * cmd->resp[1] holds any four-byte response, for R3 (READ_OCR) and on
  161. * newer cards R7 (IF_COND).
  162. */
  163. static char *maptype(struct mmc_command *cmd)
  164. {
  165. switch (mmc_spi_resp_type(cmd)) {
  166. case MMC_RSP_SPI_R1: return "R1";
  167. case MMC_RSP_SPI_R1B: return "R1B";
  168. case MMC_RSP_SPI_R2: return "R2/R5";
  169. case MMC_RSP_SPI_R3: return "R3/R4/R7";
  170. default: return "?";
  171. }
  172. }
  173. /* return zero, else negative errno after setting cmd->error */
  174. static int mmc_spi_response_get(struct mmc_spi_host *host,
  175. struct mmc_command *cmd, int cs_on)
  176. {
  177. unsigned long timeout_ms;
  178. u8 *cp = host->data->status;
  179. u8 *end = cp + host->t.len;
  180. int value = 0;
  181. int bitshift;
  182. u8 leftover = 0;
  183. unsigned short rotator;
  184. int i;
  185. /* Except for data block reads, the whole response will already
  186. * be stored in the scratch buffer. It's somewhere after the
  187. * command and the first byte we read after it. We ignore that
  188. * first byte. After STOP_TRANSMISSION command it may include
  189. * two data bits, but otherwise it's all ones.
  190. */
  191. cp += 8;
  192. while (cp < end && *cp == 0xff)
  193. cp++;
  194. /* Data block reads (R1 response types) may need more data... */
  195. if (cp == end) {
  196. cp = host->data->status;
  197. end = cp+1;
  198. /* Card sends N(CR) (== 1..8) bytes of all-ones then one
  199. * status byte ... and we already scanned 2 bytes.
  200. *
  201. * REVISIT block read paths use nasty byte-at-a-time I/O
  202. * so it can always DMA directly into the target buffer.
  203. * It'd probably be better to memcpy() the first chunk and
  204. * avoid extra i/o calls...
  205. *
  206. * Note we check for more than 8 bytes, because in practice,
  207. * some SD cards are slow...
  208. */
  209. for (i = 2; i < 16; i++) {
  210. value = mmc_spi_readbytes(host, 1);
  211. if (value < 0)
  212. goto done;
  213. if (*cp != 0xff)
  214. goto checkstatus;
  215. }
  216. value = -ETIMEDOUT;
  217. goto done;
  218. }
  219. checkstatus:
  220. bitshift = 0;
  221. if (*cp & 0x80) {
  222. /* Houston, we have an ugly card with a bit-shifted response */
  223. rotator = *cp++ << 8;
  224. /* read the next byte */
  225. if (cp == end) {
  226. value = mmc_spi_readbytes(host, 1);
  227. if (value < 0)
  228. goto done;
  229. cp = host->data->status;
  230. end = cp+1;
  231. }
  232. rotator |= *cp++;
  233. while (rotator & 0x8000) {
  234. bitshift++;
  235. rotator <<= 1;
  236. }
  237. cmd->resp[0] = rotator >> 8;
  238. leftover = rotator;
  239. } else {
  240. cmd->resp[0] = *cp++;
  241. }
  242. cmd->error = 0;
  243. /* Status byte: the entire seven-bit R1 response. */
  244. if (cmd->resp[0] != 0) {
  245. if ((R1_SPI_PARAMETER | R1_SPI_ADDRESS)
  246. & cmd->resp[0])
  247. value = -EFAULT; /* Bad address */
  248. else if (R1_SPI_ILLEGAL_COMMAND & cmd->resp[0])
  249. value = -ENOSYS; /* Function not implemented */
  250. else if (R1_SPI_COM_CRC & cmd->resp[0])
  251. value = -EILSEQ; /* Illegal byte sequence */
  252. else if ((R1_SPI_ERASE_SEQ | R1_SPI_ERASE_RESET)
  253. & cmd->resp[0])
  254. value = -EIO; /* I/O error */
  255. /* else R1_SPI_IDLE, "it's resetting" */
  256. }
  257. switch (mmc_spi_resp_type(cmd)) {
  258. /* SPI R1B == R1 + busy; STOP_TRANSMISSION (for multiblock reads)
  259. * and less-common stuff like various erase operations.
  260. */
  261. case MMC_RSP_SPI_R1B:
  262. /* maybe we read all the busy tokens already */
  263. while (cp < end && *cp == 0)
  264. cp++;
  265. if (cp == end) {
  266. timeout_ms = cmd->busy_timeout ? cmd->busy_timeout :
  267. MMC_SPI_R1B_TIMEOUT_MS;
  268. mmc_spi_wait_unbusy(host, msecs_to_jiffies(timeout_ms));
  269. }
  270. break;
  271. /* SPI R2 == R1 + second status byte; SEND_STATUS
  272. * SPI R5 == R1 + data byte; IO_RW_DIRECT
  273. */
  274. case MMC_RSP_SPI_R2:
  275. /* read the next byte */
  276. if (cp == end) {
  277. value = mmc_spi_readbytes(host, 1);
  278. if (value < 0)
  279. goto done;
  280. cp = host->data->status;
  281. end = cp+1;
  282. }
  283. if (bitshift) {
  284. rotator = leftover << 8;
  285. rotator |= *cp << bitshift;
  286. cmd->resp[0] |= (rotator & 0xFF00);
  287. } else {
  288. cmd->resp[0] |= *cp << 8;
  289. }
  290. break;
  291. /* SPI R3, R4, or R7 == R1 + 4 bytes */
  292. case MMC_RSP_SPI_R3:
  293. rotator = leftover << 8;
  294. cmd->resp[1] = 0;
  295. for (i = 0; i < 4; i++) {
  296. cmd->resp[1] <<= 8;
  297. /* read the next byte */
  298. if (cp == end) {
  299. value = mmc_spi_readbytes(host, 1);
  300. if (value < 0)
  301. goto done;
  302. cp = host->data->status;
  303. end = cp+1;
  304. }
  305. if (bitshift) {
  306. rotator |= *cp++ << bitshift;
  307. cmd->resp[1] |= (rotator >> 8);
  308. rotator <<= 8;
  309. } else {
  310. cmd->resp[1] |= *cp++;
  311. }
  312. }
  313. break;
  314. /* SPI R1 == just one status byte */
  315. case MMC_RSP_SPI_R1:
  316. break;
  317. default:
  318. dev_dbg(&host->spi->dev, "bad response type %04x\n",
  319. mmc_spi_resp_type(cmd));
  320. if (value >= 0)
  321. value = -EINVAL;
  322. goto done;
  323. }
  324. if (value < 0)
  325. dev_dbg(&host->spi->dev,
  326. " ... CMD%d response SPI_%s: resp %04x %08x\n",
  327. cmd->opcode, maptype(cmd), cmd->resp[0], cmd->resp[1]);
  328. /* disable chipselect on errors and some success cases */
  329. if (value >= 0 && cs_on)
  330. return value;
  331. done:
  332. if (value < 0)
  333. cmd->error = value;
  334. mmc_cs_off(host);
  335. return value;
  336. }
  337. /* Issue command and read its response.
  338. * Returns zero on success, negative for error.
  339. *
  340. * On error, caller must cope with mmc core retry mechanism. That
  341. * means immediate low-level resubmit, which affects the bus lock...
  342. */
  343. static int
  344. mmc_spi_command_send(struct mmc_spi_host *host,
  345. struct mmc_request *mrq,
  346. struct mmc_command *cmd, int cs_on)
  347. {
  348. struct scratch *data = host->data;
  349. u8 *cp = data->status;
  350. int status;
  351. struct spi_transfer *t;
  352. /* We can handle most commands (except block reads) in one full
  353. * duplex I/O operation before either starting the next transfer
  354. * (data block or command) or else deselecting the card.
  355. *
  356. * First, write 7 bytes:
  357. * - an all-ones byte to ensure the card is ready
  358. * - opcode byte (plus start and transmission bits)
  359. * - four bytes of big-endian argument
  360. * - crc7 (plus end bit) ... always computed, it's cheap
  361. *
  362. * We init the whole buffer to all-ones, which is what we need
  363. * to write while we're reading (later) response data.
  364. */
  365. memset(cp, 0xff, sizeof(data->status));
  366. cp[1] = 0x40 | cmd->opcode;
  367. put_unaligned_be32(cmd->arg, cp + 2);
  368. cp[6] = crc7_be(0, cp + 1, 5) | 0x01;
  369. cp += 7;
  370. /* Then, read up to 13 bytes (while writing all-ones):
  371. * - N(CR) (== 1..8) bytes of all-ones
  372. * - status byte (for all response types)
  373. * - the rest of the response, either:
  374. * + nothing, for R1 or R1B responses
  375. * + second status byte, for R2 responses
  376. * + four data bytes, for R3 and R7 responses
  377. *
  378. * Finally, read some more bytes ... in the nice cases we know in
  379. * advance how many, and reading 1 more is always OK:
  380. * - N(EC) (== 0..N) bytes of all-ones, before deselect/finish
  381. * - N(RC) (== 1..N) bytes of all-ones, before next command
  382. * - N(WR) (== 1..N) bytes of all-ones, before data write
  383. *
  384. * So in those cases one full duplex I/O of at most 21 bytes will
  385. * handle the whole command, leaving the card ready to receive a
  386. * data block or new command. We do that whenever we can, shaving
  387. * CPU and IRQ costs (especially when using DMA or FIFOs).
  388. *
  389. * There are two other cases, where it's not generally practical
  390. * to rely on a single I/O:
  391. *
  392. * - R1B responses need at least N(EC) bytes of all-zeroes.
  393. *
  394. * In this case we can *try* to fit it into one I/O, then
  395. * maybe read more data later.
  396. *
  397. * - Data block reads are more troublesome, since a variable
  398. * number of padding bytes precede the token and data.
  399. * + N(CX) (== 0..8) bytes of all-ones, before CSD or CID
  400. * + N(AC) (== 1..many) bytes of all-ones
  401. *
  402. * In this case we currently only have minimal speedups here:
  403. * when N(CR) == 1 we can avoid I/O in response_get().
  404. */
  405. if (cs_on && (mrq->data->flags & MMC_DATA_READ)) {
  406. cp += 2; /* min(N(CR)) + status */
  407. /* R1 */
  408. } else {
  409. cp += 10; /* max(N(CR)) + status + min(N(RC),N(WR)) */
  410. if (cmd->flags & MMC_RSP_SPI_S2) /* R2/R5 */
  411. cp++;
  412. else if (cmd->flags & MMC_RSP_SPI_B4) /* R3/R4/R7 */
  413. cp += 4;
  414. else if (cmd->flags & MMC_RSP_BUSY) /* R1B */
  415. cp = data->status + sizeof(data->status);
  416. /* else: R1 (most commands) */
  417. }
  418. dev_dbg(&host->spi->dev, " CMD%d, resp %s\n",
  419. cmd->opcode, maptype(cmd));
  420. /* send command, leaving chipselect active */
  421. spi_message_init(&host->m);
  422. t = &host->t;
  423. memset(t, 0, sizeof(*t));
  424. t->tx_buf = t->rx_buf = data->status;
  425. t->len = cp - data->status;
  426. t->cs_change = 1;
  427. spi_message_add_tail(t, &host->m);
  428. status = spi_sync_locked(host->spi, &host->m);
  429. if (status < 0) {
  430. dev_dbg(&host->spi->dev, " ... write returned %d\n", status);
  431. cmd->error = status;
  432. return status;
  433. }
  434. /* after no-data commands and STOP_TRANSMISSION, chipselect off */
  435. return mmc_spi_response_get(host, cmd, cs_on);
  436. }
  437. /* Build data message with up to four separate transfers. For TX, we
  438. * start by writing the data token. And in most cases, we finish with
  439. * a status transfer.
  440. *
  441. * We always provide TX data for data and CRC. The MMC/SD protocol
  442. * requires us to write ones; but Linux defaults to writing zeroes;
  443. * so we explicitly initialize it to all ones on RX paths.
  444. */
  445. static void
  446. mmc_spi_setup_data_message(struct mmc_spi_host *host, bool multiple, bool write)
  447. {
  448. struct spi_transfer *t;
  449. struct scratch *scratch = host->data;
  450. spi_message_init(&host->m);
  451. /* for reads, readblock() skips 0xff bytes before finding
  452. * the token; for writes, this transfer issues that token.
  453. */
  454. if (write) {
  455. t = &host->token;
  456. memset(t, 0, sizeof(*t));
  457. t->len = 1;
  458. if (multiple)
  459. scratch->data_token = SPI_TOKEN_MULTI_WRITE;
  460. else
  461. scratch->data_token = SPI_TOKEN_SINGLE;
  462. t->tx_buf = &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. /* length and actual buffer info are written later */
  472. spi_message_add_tail(t, &host->m);
  473. t = &host->crc;
  474. memset(t, 0, sizeof(*t));
  475. t->len = 2;
  476. if (write) {
  477. /* the actual CRC may get written later */
  478. t->tx_buf = &scratch->crc_val;
  479. } else {
  480. t->tx_buf = host->ones;
  481. t->rx_buf = &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 || write) {
  499. t = &host->early_status;
  500. memset(t, 0, sizeof(*t));
  501. t->len = write ? sizeof(scratch->status) : 1;
  502. t->tx_buf = host->ones;
  503. t->rx_buf = scratch->status;
  504. t->cs_change = 1;
  505. spi_message_add_tail(t, &host->m);
  506. }
  507. }
  508. /*
  509. * Write one block:
  510. * - caller handled preceding N(WR) [1+] all-ones bytes
  511. * - data block
  512. * + token
  513. * + data bytes
  514. * + crc16
  515. * - an all-ones byte ... card writes a data-response byte
  516. * - followed by N(EC) [0+] all-ones bytes, card writes zero/'busy'
  517. *
  518. * Return negative errno, else success.
  519. */
  520. static int
  521. mmc_spi_writeblock(struct mmc_spi_host *host, struct spi_transfer *t,
  522. unsigned long timeout)
  523. {
  524. struct spi_device *spi = host->spi;
  525. int status, i;
  526. struct scratch *scratch = host->data;
  527. u32 pattern;
  528. if (host->mmc->use_spi_crc)
  529. scratch->crc_val = cpu_to_be16(crc_itu_t(0, t->tx_buf, t->len));
  530. status = spi_sync_locked(spi, &host->m);
  531. if (status != 0) {
  532. dev_dbg(&spi->dev, "write error (%d)\n", status);
  533. return status;
  534. }
  535. /*
  536. * Get the transmission data-response reply. It must follow
  537. * immediately after the data block we transferred. This reply
  538. * doesn't necessarily tell whether the write operation succeeded;
  539. * it just says if the transmission was ok and whether *earlier*
  540. * writes succeeded; see the standard.
  541. *
  542. * In practice, there are (even modern SDHC-)cards which are late
  543. * in sending the response, and miss the time frame by a few bits,
  544. * so we have to cope with this situation and check the response
  545. * bit-by-bit. Arggh!!!
  546. */
  547. pattern = get_unaligned_be32(scratch->status);
  548. /* First 3 bit of pattern are undefined */
  549. pattern |= 0xE0000000;
  550. /* left-adjust to leading 0 bit */
  551. while (pattern & 0x80000000)
  552. pattern <<= 1;
  553. /* right-adjust for pattern matching. Code is in bit 4..0 now. */
  554. pattern >>= 27;
  555. switch (pattern) {
  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. /* Return when not busy. If we didn't collect that status yet,
  580. * we'll need some more I/O.
  581. */
  582. for (i = 4; i < sizeof(scratch->status); i++) {
  583. /* card is non-busy if the most recent bit is 1 */
  584. if (scratch->status[i] & 0x01)
  585. return 0;
  586. }
  587. return mmc_spi_wait_unbusy(host, timeout);
  588. }
  589. /*
  590. * Read one block:
  591. * - skip leading all-ones bytes ... either
  592. * + N(AC) [1..f(clock,CSD)] usually, else
  593. * + N(CX) [0..8] when reading CSD or CID
  594. * - data block
  595. * + token ... if error token, no data or crc
  596. * + data bytes
  597. * + crc16
  598. *
  599. * After single block reads, we're done; N(EC) [0+] all-ones bytes follow
  600. * before dropping chipselect.
  601. *
  602. * For multiblock reads, caller either reads the next block or issues a
  603. * STOP_TRANSMISSION command.
  604. */
  605. static int
  606. mmc_spi_readblock(struct mmc_spi_host *host, struct spi_transfer *t,
  607. unsigned long timeout)
  608. {
  609. struct spi_device *spi = host->spi;
  610. int status;
  611. struct scratch *scratch = host->data;
  612. unsigned int bitshift;
  613. u8 leftover;
  614. /* At least one SD card sends an all-zeroes byte when N(CX)
  615. * applies, before the all-ones bytes ... just cope with that.
  616. */
  617. status = mmc_spi_readbytes(host, 1);
  618. if (status < 0)
  619. return status;
  620. status = scratch->status[0];
  621. if (status == 0xff || status == 0)
  622. status = mmc_spi_readtoken(host, timeout);
  623. if (status < 0) {
  624. dev_dbg(&spi->dev, "read error %02x (%d)\n", status, status);
  625. return status;
  626. }
  627. /* The token may be bit-shifted...
  628. * the first 0-bit precedes the data stream.
  629. */
  630. bitshift = 7;
  631. while (status & 0x80) {
  632. status <<= 1;
  633. bitshift--;
  634. }
  635. leftover = status << 1;
  636. status = spi_sync_locked(spi, &host->m);
  637. if (status < 0) {
  638. dev_dbg(&spi->dev, "read error %d\n", status);
  639. return status;
  640. }
  641. if (bitshift) {
  642. /* Walk through the data and the crc and do
  643. * all the magic to get byte-aligned data.
  644. */
  645. u8 *cp = t->rx_buf;
  646. unsigned int len;
  647. unsigned int bitright = 8 - bitshift;
  648. u8 temp;
  649. for (len = t->len; len; len--) {
  650. temp = *cp;
  651. *cp++ = leftover | (temp >> bitshift);
  652. leftover = temp << bitright;
  653. }
  654. cp = (u8 *) &scratch->crc_val;
  655. temp = *cp;
  656. *cp++ = leftover | (temp >> bitshift);
  657. leftover = temp << bitright;
  658. temp = *cp;
  659. *cp = leftover | (temp >> bitshift);
  660. }
  661. if (host->mmc->use_spi_crc) {
  662. u16 crc = crc_itu_t(0, t->rx_buf, t->len);
  663. be16_to_cpus(&scratch->crc_val);
  664. if (scratch->crc_val != crc) {
  665. dev_dbg(&spi->dev,
  666. "read - crc error: crc_val=0x%04x, computed=0x%04x len=%d\n",
  667. scratch->crc_val, crc, t->len);
  668. return -EILSEQ;
  669. }
  670. }
  671. t->rx_buf += t->len;
  672. return 0;
  673. }
  674. /*
  675. * An MMC/SD data stage includes one or more blocks, optional CRCs,
  676. * and inline handshaking. That handhaking makes it unlike most
  677. * other SPI protocol stacks.
  678. */
  679. static void
  680. mmc_spi_data_do(struct mmc_spi_host *host, struct mmc_command *cmd,
  681. struct mmc_data *data, u32 blk_size)
  682. {
  683. struct spi_device *spi = host->spi;
  684. struct spi_transfer *t;
  685. struct scatterlist *sg;
  686. unsigned n_sg;
  687. bool multiple = (data->blocks > 1);
  688. bool write = (data->flags & MMC_DATA_WRITE);
  689. const char *write_or_read = write ? "write" : "read";
  690. u32 clock_rate;
  691. unsigned long timeout;
  692. mmc_spi_setup_data_message(host, multiple, write);
  693. t = &host->t;
  694. if (t->speed_hz)
  695. clock_rate = t->speed_hz;
  696. else
  697. clock_rate = spi->max_speed_hz;
  698. timeout = data->timeout_ns / 1000 +
  699. data->timeout_clks * 1000000 / clock_rate;
  700. timeout = usecs_to_jiffies((unsigned int)timeout) + 1;
  701. /* Handle scatterlist segments one at a time, with synch for
  702. * each 512-byte block
  703. */
  704. for_each_sg(data->sg, sg, data->sg_len, n_sg) {
  705. int status = 0;
  706. void *kmap_addr;
  707. unsigned length = sg->length;
  708. /* allow pio too; we don't allow highmem */
  709. kmap_addr = kmap(sg_page(sg));
  710. if (write)
  711. t->tx_buf = kmap_addr + sg->offset;
  712. else
  713. t->rx_buf = kmap_addr + sg->offset;
  714. /* transfer each block, and update request status */
  715. while (length) {
  716. t->len = min(length, blk_size);
  717. dev_dbg(&spi->dev, " %s block, %d bytes\n", write_or_read, t->len);
  718. if (write)
  719. status = mmc_spi_writeblock(host, t, timeout);
  720. else
  721. status = mmc_spi_readblock(host, t, timeout);
  722. if (status < 0)
  723. break;
  724. data->bytes_xfered += t->len;
  725. length -= t->len;
  726. if (!multiple)
  727. break;
  728. }
  729. /* discard mappings */
  730. if (write)
  731. /* nothing to do */;
  732. else
  733. flush_dcache_page(sg_page(sg));
  734. kunmap(sg_page(sg));
  735. if (status < 0) {
  736. data->error = status;
  737. dev_dbg(&spi->dev, "%s status %d\n", write_or_read, status);
  738. break;
  739. }
  740. }
  741. /* NOTE some docs describe an MMC-only SET_BLOCK_COUNT (CMD23) that
  742. * can be issued before multiblock writes. Unlike its more widely
  743. * documented analogue for SD cards (SET_WR_BLK_ERASE_COUNT, ACMD23),
  744. * that can affect the STOP_TRAN logic. Complete (and current)
  745. * MMC specs should sort that out before Linux starts using CMD23.
  746. */
  747. if (write && multiple) {
  748. struct scratch *scratch = host->data;
  749. int tmp;
  750. const unsigned statlen = sizeof(scratch->status);
  751. dev_dbg(&spi->dev, " STOP_TRAN\n");
  752. /* Tweak the per-block message we set up earlier by morphing
  753. * it to hold single buffer with the token followed by some
  754. * all-ones bytes ... skip N(BR) (0..1), scan the rest for
  755. * "not busy any longer" status, and leave chip selected.
  756. */
  757. INIT_LIST_HEAD(&host->m.transfers);
  758. list_add(&host->early_status.transfer_list,
  759. &host->m.transfers);
  760. memset(scratch->status, 0xff, statlen);
  761. scratch->status[0] = SPI_TOKEN_STOP_TRAN;
  762. host->early_status.tx_buf = host->early_status.rx_buf;
  763. host->early_status.len = statlen;
  764. tmp = spi_sync_locked(spi, &host->m);
  765. if (tmp < 0) {
  766. if (!data->error)
  767. data->error = tmp;
  768. return;
  769. }
  770. /* Ideally we collected "not busy" status with one I/O,
  771. * avoiding wasteful byte-at-a-time scanning... but more
  772. * I/O is often needed.
  773. */
  774. for (tmp = 2; tmp < statlen; tmp++) {
  775. if (scratch->status[tmp] != 0)
  776. return;
  777. }
  778. tmp = mmc_spi_wait_unbusy(host, timeout);
  779. if (tmp < 0 && !data->error)
  780. data->error = tmp;
  781. }
  782. }
  783. /****************************************************************************/
  784. /*
  785. * MMC driver implementation -- the interface to the MMC stack
  786. */
  787. static void mmc_spi_request(struct mmc_host *mmc, struct mmc_request *mrq)
  788. {
  789. struct mmc_spi_host *host = mmc_priv(mmc);
  790. int status = -EINVAL;
  791. int crc_retry = 5;
  792. struct mmc_command stop;
  793. #ifdef DEBUG
  794. /* MMC core and layered drivers *MUST* issue SPI-aware commands */
  795. {
  796. struct mmc_command *cmd;
  797. int invalid = 0;
  798. cmd = mrq->cmd;
  799. if (!mmc_spi_resp_type(cmd)) {
  800. dev_dbg(&host->spi->dev, "bogus command\n");
  801. cmd->error = -EINVAL;
  802. invalid = 1;
  803. }
  804. cmd = mrq->stop;
  805. if (cmd && !mmc_spi_resp_type(cmd)) {
  806. dev_dbg(&host->spi->dev, "bogus STOP command\n");
  807. cmd->error = -EINVAL;
  808. invalid = 1;
  809. }
  810. if (invalid) {
  811. dump_stack();
  812. mmc_request_done(host->mmc, mrq);
  813. return;
  814. }
  815. }
  816. #endif
  817. /* request exclusive bus access */
  818. spi_bus_lock(host->spi->controller);
  819. crc_recover:
  820. /* issue command; then optionally data and stop */
  821. status = mmc_spi_command_send(host, mrq, mrq->cmd, mrq->data != NULL);
  822. if (status == 0 && mrq->data) {
  823. mmc_spi_data_do(host, mrq->cmd, mrq->data, mrq->data->blksz);
  824. /*
  825. * The SPI bus is not always reliable for large data transfers.
  826. * If an occasional crc error is reported by the SD device with
  827. * data read/write over SPI, it may be recovered by repeating
  828. * the last SD command again. The retry count is set to 5 to
  829. * ensure the driver passes stress tests.
  830. */
  831. if (mrq->data->error == -EILSEQ && crc_retry) {
  832. stop.opcode = MMC_STOP_TRANSMISSION;
  833. stop.arg = 0;
  834. stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  835. status = mmc_spi_command_send(host, mrq, &stop, 0);
  836. crc_retry--;
  837. mrq->data->error = 0;
  838. goto crc_recover;
  839. }
  840. if (mrq->stop)
  841. status = mmc_spi_command_send(host, mrq, mrq->stop, 0);
  842. else
  843. mmc_cs_off(host);
  844. }
  845. /* release the bus */
  846. spi_bus_unlock(host->spi->controller);
  847. mmc_request_done(host->mmc, mrq);
  848. }
  849. /* See Section 6.4.1, in SD "Simplified Physical Layer Specification 2.0"
  850. *
  851. * NOTE that here we can't know that the card has just been powered up;
  852. * not all MMC/SD sockets support power switching.
  853. *
  854. * FIXME when the card is still in SPI mode, e.g. from a previous kernel,
  855. * this doesn't seem to do the right thing at all...
  856. */
  857. static void mmc_spi_initsequence(struct mmc_spi_host *host)
  858. {
  859. /* Try to be very sure any previous command has completed;
  860. * wait till not-busy, skip debris from any old commands.
  861. */
  862. mmc_spi_wait_unbusy(host, msecs_to_jiffies(MMC_SPI_INIT_TIMEOUT_MS));
  863. mmc_spi_readbytes(host, 10);
  864. /*
  865. * Do a burst with chipselect active-high. We need to do this to
  866. * meet the requirement of 74 clock cycles with both chipselect
  867. * and CMD (MOSI) high before CMD0 ... after the card has been
  868. * powered up to Vdd(min), and so is ready to take commands.
  869. *
  870. * Some cards are particularly needy of this (e.g. Viking "SD256")
  871. * while most others don't seem to care.
  872. *
  873. * Note that this is one of the places MMC/SD plays games with the
  874. * SPI protocol. Another is that when chipselect is released while
  875. * the card returns BUSY status, the clock must issue several cycles
  876. * with chipselect high before the card will stop driving its output.
  877. *
  878. * SPI_CS_HIGH means "asserted" here. In some cases like when using
  879. * GPIOs for chip select, SPI_CS_HIGH is set but this will be logically
  880. * inverted by gpiolib, so if we want to ascertain to drive it high
  881. * we should toggle the default with an XOR as we do here.
  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, "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 failed\n");
  970. }
  971. }
  972. host->power_mode = ios->power_mode;
  973. }
  974. if (host->spi->max_speed_hz != ios->clock && ios->clock != 0) {
  975. int status;
  976. host->spi->max_speed_hz = ios->clock;
  977. status = spi_setup(host->spi);
  978. dev_dbg(&host->spi->dev, " clock to %d Hz, %d\n",
  979. host->spi->max_speed_hz, status);
  980. }
  981. }
  982. static const struct mmc_host_ops mmc_spi_ops = {
  983. .request = mmc_spi_request,
  984. .set_ios = mmc_spi_set_ios,
  985. .get_ro = mmc_gpio_get_ro,
  986. .get_cd = mmc_gpio_get_cd,
  987. };
  988. /****************************************************************************/
  989. /*
  990. * SPI driver implementation
  991. */
  992. static irqreturn_t
  993. mmc_spi_detect_irq(int irq, void *mmc)
  994. {
  995. struct mmc_spi_host *host = mmc_priv(mmc);
  996. u16 delay_msec = max(host->pdata->detect_delay, (u16)100);
  997. mmc_detect_change(mmc, msecs_to_jiffies(delay_msec));
  998. return IRQ_HANDLED;
  999. }
  1000. static int mmc_spi_probe(struct spi_device *spi)
  1001. {
  1002. void *ones;
  1003. struct mmc_host *mmc;
  1004. struct mmc_spi_host *host;
  1005. int status;
  1006. bool has_ro = false;
  1007. /* We rely on full duplex transfers, mostly to reduce
  1008. * per-transfer overheads (by making fewer transfers).
  1009. */
  1010. if (spi->controller->flags & SPI_CONTROLLER_HALF_DUPLEX)
  1011. return -EINVAL;
  1012. /* MMC and SD specs only seem to care that sampling is on the
  1013. * rising edge ... meaning SPI modes 0 or 3. So either SPI mode
  1014. * should be legit. We'll use mode 0 since the steady state is 0,
  1015. * which is appropriate for hotplugging, unless the platform data
  1016. * specify mode 3 (if hardware is not compatible to mode 0).
  1017. */
  1018. if (spi->mode != SPI_MODE_3)
  1019. spi->mode = SPI_MODE_0;
  1020. spi->bits_per_word = 8;
  1021. status = spi_setup(spi);
  1022. if (status < 0) {
  1023. dev_dbg(&spi->dev, "needs SPI mode %02x, %d KHz; %d\n",
  1024. spi->mode, spi->max_speed_hz / 1000,
  1025. status);
  1026. return status;
  1027. }
  1028. /* We need a supply of ones to transmit. This is the only time
  1029. * the CPU touches these, so cache coherency isn't a concern.
  1030. *
  1031. * NOTE if many systems use more than one MMC-over-SPI connector
  1032. * it'd save some memory to share this. That's evidently rare.
  1033. */
  1034. status = -ENOMEM;
  1035. ones = kmalloc(MMC_SPI_BLOCKSIZE, GFP_KERNEL);
  1036. if (!ones)
  1037. goto nomem;
  1038. memset(ones, 0xff, MMC_SPI_BLOCKSIZE);
  1039. mmc = mmc_alloc_host(sizeof(*host), &spi->dev);
  1040. if (!mmc)
  1041. goto nomem;
  1042. mmc->ops = &mmc_spi_ops;
  1043. mmc->max_blk_size = MMC_SPI_BLOCKSIZE;
  1044. mmc->max_segs = MMC_SPI_BLOCKSATONCE;
  1045. mmc->max_req_size = MMC_SPI_BLOCKSATONCE * MMC_SPI_BLOCKSIZE;
  1046. mmc->max_blk_count = MMC_SPI_BLOCKSATONCE;
  1047. mmc->caps = MMC_CAP_SPI;
  1048. /* SPI doesn't need the lowspeed device identification thing for
  1049. * MMC or SD cards, since it never comes up in open drain mode.
  1050. * That's good; some SPI masters can't handle very low speeds!
  1051. *
  1052. * However, low speed SDIO cards need not handle over 400 KHz;
  1053. * that's the only reason not to use a few MHz for f_min (until
  1054. * the upper layer reads the target frequency from the CSD).
  1055. */
  1056. if (spi->controller->min_speed_hz > 400000)
  1057. dev_warn(&spi->dev,"Controller unable to reduce bus clock to 400 KHz\n");
  1058. mmc->f_min = max(spi->controller->min_speed_hz, 400000);
  1059. mmc->f_max = spi->max_speed_hz;
  1060. host = mmc_priv(mmc);
  1061. host->mmc = mmc;
  1062. host->spi = spi;
  1063. host->ones = ones;
  1064. dev_set_drvdata(&spi->dev, mmc);
  1065. /* Platform data is used to hook up things like card sensing
  1066. * and power switching gpios.
  1067. */
  1068. host->pdata = mmc_spi_get_pdata(spi);
  1069. if (host->pdata)
  1070. mmc->ocr_avail = host->pdata->ocr_mask;
  1071. if (!mmc->ocr_avail) {
  1072. dev_warn(&spi->dev, "ASSUMING 3.2-3.4 V slot power\n");
  1073. mmc->ocr_avail = MMC_VDD_32_33|MMC_VDD_33_34;
  1074. }
  1075. if (host->pdata && host->pdata->setpower) {
  1076. host->powerup_msecs = host->pdata->powerup_msecs;
  1077. if (!host->powerup_msecs || host->powerup_msecs > 250)
  1078. host->powerup_msecs = 250;
  1079. }
  1080. /* Preallocate buffers */
  1081. host->data = kmalloc(sizeof(*host->data), GFP_KERNEL);
  1082. if (!host->data)
  1083. goto fail_nobuf1;
  1084. /* setup message for status/busy readback */
  1085. spi_message_init(&host->readback);
  1086. spi_message_add_tail(&host->status, &host->readback);
  1087. host->status.tx_buf = host->ones;
  1088. host->status.rx_buf = &host->data->status;
  1089. host->status.cs_change = 1;
  1090. /* register card detect irq */
  1091. if (host->pdata && host->pdata->init) {
  1092. status = host->pdata->init(&spi->dev, mmc_spi_detect_irq, mmc);
  1093. if (status != 0)
  1094. goto fail_glue_init;
  1095. }
  1096. /* pass platform capabilities, if any */
  1097. if (host->pdata) {
  1098. mmc->caps |= host->pdata->caps;
  1099. mmc->caps2 |= host->pdata->caps2;
  1100. }
  1101. status = mmc_add_host(mmc);
  1102. if (status != 0)
  1103. goto fail_glue_init;
  1104. /*
  1105. * Index 0 is card detect
  1106. * Old boardfiles were specifying 1 ms as debounce
  1107. */
  1108. status = mmc_gpiod_request_cd(mmc, NULL, 0, false, 1000);
  1109. if (status == -EPROBE_DEFER)
  1110. goto fail_gpiod_request;
  1111. if (!status) {
  1112. /*
  1113. * The platform has a CD GPIO signal that may support
  1114. * interrupts, so let mmc_gpiod_request_cd_irq() decide
  1115. * if polling is needed or not.
  1116. */
  1117. mmc->caps &= ~MMC_CAP_NEEDS_POLL;
  1118. mmc_gpiod_request_cd_irq(mmc);
  1119. }
  1120. mmc_detect_change(mmc, 0);
  1121. /* Index 1 is write protect/read only */
  1122. status = mmc_gpiod_request_ro(mmc, NULL, 1, 0);
  1123. if (status == -EPROBE_DEFER)
  1124. goto fail_gpiod_request;
  1125. if (!status)
  1126. has_ro = true;
  1127. dev_info(&spi->dev, "SD/MMC host %s%s%s%s\n",
  1128. dev_name(&mmc->class_dev),
  1129. has_ro ? "" : ", no WP",
  1130. (host->pdata && host->pdata->setpower)
  1131. ? "" : ", no poweroff",
  1132. (mmc->caps & MMC_CAP_NEEDS_POLL)
  1133. ? ", cd polling" : "");
  1134. return 0;
  1135. fail_gpiod_request:
  1136. mmc_remove_host(mmc);
  1137. fail_glue_init:
  1138. kfree(host->data);
  1139. fail_nobuf1:
  1140. mmc_spi_put_pdata(spi);
  1141. mmc_free_host(mmc);
  1142. nomem:
  1143. kfree(ones);
  1144. return status;
  1145. }
  1146. static void mmc_spi_remove(struct spi_device *spi)
  1147. {
  1148. struct mmc_host *mmc = dev_get_drvdata(&spi->dev);
  1149. struct mmc_spi_host *host = mmc_priv(mmc);
  1150. /* prevent new mmc_detect_change() calls */
  1151. if (host->pdata && host->pdata->exit)
  1152. host->pdata->exit(&spi->dev, mmc);
  1153. mmc_remove_host(mmc);
  1154. kfree(host->data);
  1155. kfree(host->ones);
  1156. spi->max_speed_hz = mmc->f_max;
  1157. mmc_spi_put_pdata(spi);
  1158. mmc_free_host(mmc);
  1159. }
  1160. static const struct spi_device_id mmc_spi_dev_ids[] = {
  1161. { "mmc-spi-slot"},
  1162. { },
  1163. };
  1164. MODULE_DEVICE_TABLE(spi, mmc_spi_dev_ids);
  1165. static const struct of_device_id mmc_spi_of_match_table[] = {
  1166. { .compatible = "mmc-spi-slot", },
  1167. {},
  1168. };
  1169. MODULE_DEVICE_TABLE(of, mmc_spi_of_match_table);
  1170. static struct spi_driver mmc_spi_driver = {
  1171. .driver = {
  1172. .name = "mmc_spi",
  1173. .of_match_table = mmc_spi_of_match_table,
  1174. },
  1175. .id_table = mmc_spi_dev_ids,
  1176. .probe = mmc_spi_probe,
  1177. .remove = mmc_spi_remove,
  1178. };
  1179. module_spi_driver(mmc_spi_driver);
  1180. MODULE_AUTHOR("Mike Lavender, David Brownell, Hans-Peter Nilsson, Jan Nikitenko");
  1181. MODULE_DESCRIPTION("SPI SD/MMC host driver");
  1182. MODULE_LICENSE("GPL");
  1183. MODULE_ALIAS("spi:mmc_spi");