sdio_io.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. /*
  2. * linux/drivers/mmc/core/sdio_io.c
  3. *
  4. * Copyright 2007-2008 Pierre Ossman
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at
  9. * your option) any later version.
  10. */
  11. #include <linux/export.h>
  12. #include <linux/mmc/host.h>
  13. #include <linux/mmc/card.h>
  14. #include <linux/mmc/sdio.h>
  15. #include <linux/mmc/sdio_func.h>
  16. #include "sdio_ops.h"
  17. #include "core.h"
  18. #include "card.h"
  19. #include "host.h"
  20. /**
  21. * sdio_claim_host - exclusively claim a bus for a certain SDIO function
  22. * @func: SDIO function that will be accessed
  23. *
  24. * Claim a bus for a set of operations. The SDIO function given
  25. * is used to figure out which bus is relevant.
  26. */
  27. void sdio_claim_host(struct sdio_func *func)
  28. {
  29. if (WARN_ON(!func))
  30. return;
  31. mmc_claim_host(func->card->host);
  32. }
  33. EXPORT_SYMBOL_GPL(sdio_claim_host);
  34. /**
  35. * sdio_release_host - release a bus for a certain SDIO function
  36. * @func: SDIO function that was accessed
  37. *
  38. * Release a bus, allowing others to claim the bus for their
  39. * operations.
  40. */
  41. void sdio_release_host(struct sdio_func *func)
  42. {
  43. if (WARN_ON(!func))
  44. return;
  45. mmc_release_host(func->card->host);
  46. }
  47. EXPORT_SYMBOL_GPL(sdio_release_host);
  48. /**
  49. * sdio_enable_func - enables a SDIO function for usage
  50. * @func: SDIO function to enable
  51. *
  52. * Powers up and activates a SDIO function so that register
  53. * access is possible.
  54. */
  55. int sdio_enable_func(struct sdio_func *func)
  56. {
  57. int ret;
  58. unsigned char reg;
  59. unsigned long timeout;
  60. if (!func)
  61. return -EINVAL;
  62. pr_debug("SDIO: Enabling device %s...\n", sdio_func_id(func));
  63. ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, &reg);
  64. if (ret)
  65. goto err;
  66. reg |= 1 << func->num;
  67. ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);
  68. if (ret)
  69. goto err;
  70. timeout = jiffies + msecs_to_jiffies(func->enable_timeout);
  71. while (1) {
  72. ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IORx, 0, &reg);
  73. if (ret)
  74. goto err;
  75. if (reg & (1 << func->num))
  76. break;
  77. ret = -ETIME;
  78. if (time_after(jiffies, timeout))
  79. goto err;
  80. }
  81. pr_debug("SDIO: Enabled device %s\n", sdio_func_id(func));
  82. return 0;
  83. err:
  84. pr_debug("SDIO: Failed to enable device %s\n", sdio_func_id(func));
  85. return ret;
  86. }
  87. EXPORT_SYMBOL_GPL(sdio_enable_func);
  88. /**
  89. * sdio_disable_func - disable a SDIO function
  90. * @func: SDIO function to disable
  91. *
  92. * Powers down and deactivates a SDIO function. Register access
  93. * to this function will fail until the function is reenabled.
  94. */
  95. int sdio_disable_func(struct sdio_func *func)
  96. {
  97. int ret;
  98. unsigned char reg;
  99. if (!func)
  100. return -EINVAL;
  101. pr_debug("SDIO: Disabling device %s...\n", sdio_func_id(func));
  102. ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, &reg);
  103. if (ret)
  104. goto err;
  105. reg &= ~(1 << func->num);
  106. ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);
  107. if (ret)
  108. goto err;
  109. pr_debug("SDIO: Disabled device %s\n", sdio_func_id(func));
  110. return 0;
  111. err:
  112. pr_debug("SDIO: Failed to disable device %s\n", sdio_func_id(func));
  113. return -EIO;
  114. }
  115. EXPORT_SYMBOL_GPL(sdio_disable_func);
  116. /**
  117. * sdio_set_block_size - set the block size of an SDIO function
  118. * @func: SDIO function to change
  119. * @blksz: new block size or 0 to use the default.
  120. *
  121. * The default block size is the largest supported by both the function
  122. * and the host, with a maximum of 512 to ensure that arbitrarily sized
  123. * data transfer use the optimal (least) number of commands.
  124. *
  125. * A driver may call this to override the default block size set by the
  126. * core. This can be used to set a block size greater than the maximum
  127. * that reported by the card; it is the driver's responsibility to ensure
  128. * it uses a value that the card supports.
  129. *
  130. * Returns 0 on success, -EINVAL if the host does not support the
  131. * requested block size, or -EIO (etc.) if one of the resultant FBR block
  132. * size register writes failed.
  133. *
  134. */
  135. int sdio_set_block_size(struct sdio_func *func, unsigned blksz)
  136. {
  137. int ret;
  138. if (blksz > func->card->host->max_blk_size)
  139. return -EINVAL;
  140. if (blksz == 0) {
  141. blksz = min(func->max_blksize, func->card->host->max_blk_size);
  142. blksz = min(blksz, 512u);
  143. }
  144. ret = mmc_io_rw_direct(func->card, 1, 0,
  145. SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE,
  146. blksz & 0xff, NULL);
  147. if (ret)
  148. return ret;
  149. ret = mmc_io_rw_direct(func->card, 1, 0,
  150. SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE + 1,
  151. (blksz >> 8) & 0xff, NULL);
  152. if (ret)
  153. return ret;
  154. func->cur_blksize = blksz;
  155. return 0;
  156. }
  157. EXPORT_SYMBOL_GPL(sdio_set_block_size);
  158. /*
  159. * Calculate the maximum byte mode transfer size
  160. */
  161. static inline unsigned int sdio_max_byte_size(struct sdio_func *func)
  162. {
  163. unsigned mval = func->card->host->max_blk_size;
  164. if (mmc_blksz_for_byte_mode(func->card))
  165. mval = min(mval, func->cur_blksize);
  166. else
  167. mval = min(mval, func->max_blksize);
  168. if (mmc_card_broken_byte_mode_512(func->card))
  169. return min(mval, 511u);
  170. return min(mval, 512u); /* maximum size for byte mode */
  171. }
  172. /**
  173. * sdio_align_size - pads a transfer size to a more optimal value
  174. * @func: SDIO function
  175. * @sz: original transfer size
  176. *
  177. * Pads the original data size with a number of extra bytes in
  178. * order to avoid controller bugs and/or performance hits
  179. * (e.g. some controllers revert to PIO for certain sizes).
  180. *
  181. * If possible, it will also adjust the size so that it can be
  182. * handled in just a single request.
  183. *
  184. * Returns the improved size, which might be unmodified.
  185. */
  186. unsigned int sdio_align_size(struct sdio_func *func, unsigned int sz)
  187. {
  188. unsigned int orig_sz;
  189. unsigned int blk_sz, byte_sz;
  190. unsigned chunk_sz;
  191. orig_sz = sz;
  192. /*
  193. * Do a first check with the controller, in case it
  194. * wants to increase the size up to a point where it
  195. * might need more than one block.
  196. */
  197. sz = mmc_align_data_size(func->card, sz);
  198. /*
  199. * If we can still do this with just a byte transfer, then
  200. * we're done.
  201. */
  202. if (sz <= sdio_max_byte_size(func))
  203. return sz;
  204. if (func->card->cccr.multi_block) {
  205. /*
  206. * Check if the transfer is already block aligned
  207. */
  208. if ((sz % func->cur_blksize) == 0)
  209. return sz;
  210. /*
  211. * Realign it so that it can be done with one request,
  212. * and recheck if the controller still likes it.
  213. */
  214. blk_sz = ((sz + func->cur_blksize - 1) /
  215. func->cur_blksize) * func->cur_blksize;
  216. blk_sz = mmc_align_data_size(func->card, blk_sz);
  217. /*
  218. * This value is only good if it is still just
  219. * one request.
  220. */
  221. if ((blk_sz % func->cur_blksize) == 0)
  222. return blk_sz;
  223. /*
  224. * We failed to do one request, but at least try to
  225. * pad the remainder properly.
  226. */
  227. byte_sz = mmc_align_data_size(func->card,
  228. sz % func->cur_blksize);
  229. if (byte_sz <= sdio_max_byte_size(func)) {
  230. blk_sz = sz / func->cur_blksize;
  231. return blk_sz * func->cur_blksize + byte_sz;
  232. }
  233. } else {
  234. /*
  235. * We need multiple requests, so first check that the
  236. * controller can handle the chunk size;
  237. */
  238. chunk_sz = mmc_align_data_size(func->card,
  239. sdio_max_byte_size(func));
  240. if (chunk_sz == sdio_max_byte_size(func)) {
  241. /*
  242. * Fix up the size of the remainder (if any)
  243. */
  244. byte_sz = orig_sz % chunk_sz;
  245. if (byte_sz) {
  246. byte_sz = mmc_align_data_size(func->card,
  247. byte_sz);
  248. }
  249. return (orig_sz / chunk_sz) * chunk_sz + byte_sz;
  250. }
  251. }
  252. /*
  253. * The controller is simply incapable of transferring the size
  254. * we want in decent manner, so just return the original size.
  255. */
  256. return orig_sz;
  257. }
  258. EXPORT_SYMBOL_GPL(sdio_align_size);
  259. /* Split an arbitrarily sized data transfer into several
  260. * IO_RW_EXTENDED commands. */
  261. static int sdio_io_rw_ext_helper(struct sdio_func *func, int write,
  262. unsigned addr, int incr_addr, u8 *buf, unsigned size)
  263. {
  264. unsigned remainder = size;
  265. unsigned max_blocks;
  266. int ret;
  267. if (!func || (func->num > 7))
  268. return -EINVAL;
  269. /* Do the bulk of the transfer using block mode (if supported). */
  270. if (func->card->cccr.multi_block && (size > sdio_max_byte_size(func))) {
  271. /* Blocks per command is limited by host count, host transfer
  272. * size and the maximum for IO_RW_EXTENDED of 511 blocks. */
  273. max_blocks = min(func->card->host->max_blk_count, 511u);
  274. while (remainder >= func->cur_blksize) {
  275. unsigned blocks;
  276. blocks = remainder / func->cur_blksize;
  277. if (blocks > max_blocks)
  278. blocks = max_blocks;
  279. size = blocks * func->cur_blksize;
  280. ret = mmc_io_rw_extended(func->card, write,
  281. func->num, addr, incr_addr, buf,
  282. blocks, func->cur_blksize);
  283. if (ret)
  284. return ret;
  285. remainder -= size;
  286. buf += size;
  287. if (incr_addr)
  288. addr += size;
  289. }
  290. }
  291. /* Write the remainder using byte mode. */
  292. while (remainder > 0) {
  293. size = min(remainder, sdio_max_byte_size(func));
  294. /* Indicate byte mode by setting "blocks" = 0 */
  295. ret = mmc_io_rw_extended(func->card, write, func->num, addr,
  296. incr_addr, buf, 0, size);
  297. if (ret)
  298. return ret;
  299. remainder -= size;
  300. buf += size;
  301. if (incr_addr)
  302. addr += size;
  303. }
  304. return 0;
  305. }
  306. /**
  307. * sdio_readb - read a single byte from a SDIO function
  308. * @func: SDIO function to access
  309. * @addr: address to read
  310. * @err_ret: optional status value from transfer
  311. *
  312. * Reads a single byte from the address space of a given SDIO
  313. * function. If there is a problem reading the address, 0xff
  314. * is returned and @err_ret will contain the error code.
  315. */
  316. u8 sdio_readb(struct sdio_func *func, unsigned int addr, int *err_ret)
  317. {
  318. int ret;
  319. u8 val;
  320. if (!func) {
  321. if (err_ret)
  322. *err_ret = -EINVAL;
  323. return 0xFF;
  324. }
  325. ret = mmc_io_rw_direct(func->card, 0, func->num, addr, 0, &val);
  326. if (err_ret)
  327. *err_ret = ret;
  328. if (ret)
  329. return 0xFF;
  330. return val;
  331. }
  332. EXPORT_SYMBOL_GPL(sdio_readb);
  333. /**
  334. * sdio_writeb - write a single byte to a SDIO function
  335. * @func: SDIO function to access
  336. * @b: byte to write
  337. * @addr: address to write to
  338. * @err_ret: optional status value from transfer
  339. *
  340. * Writes a single byte to the address space of a given SDIO
  341. * function. @err_ret will contain the status of the actual
  342. * transfer.
  343. */
  344. void sdio_writeb(struct sdio_func *func, u8 b, unsigned int addr, int *err_ret)
  345. {
  346. int ret;
  347. if (!func) {
  348. if (err_ret)
  349. *err_ret = -EINVAL;
  350. return;
  351. }
  352. ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL);
  353. if (err_ret)
  354. *err_ret = ret;
  355. }
  356. EXPORT_SYMBOL_GPL(sdio_writeb);
  357. /**
  358. * sdio_writeb_readb - write and read a byte from SDIO function
  359. * @func: SDIO function to access
  360. * @write_byte: byte to write
  361. * @addr: address to write to
  362. * @err_ret: optional status value from transfer
  363. *
  364. * Performs a RAW (Read after Write) operation as defined by SDIO spec -
  365. * single byte is written to address space of a given SDIO function and
  366. * response is read back from the same address, both using single request.
  367. * If there is a problem with the operation, 0xff is returned and
  368. * @err_ret will contain the error code.
  369. */
  370. u8 sdio_writeb_readb(struct sdio_func *func, u8 write_byte,
  371. unsigned int addr, int *err_ret)
  372. {
  373. int ret;
  374. u8 val;
  375. ret = mmc_io_rw_direct(func->card, 1, func->num, addr,
  376. write_byte, &val);
  377. if (err_ret)
  378. *err_ret = ret;
  379. if (ret)
  380. return 0xff;
  381. return val;
  382. }
  383. EXPORT_SYMBOL_GPL(sdio_writeb_readb);
  384. /**
  385. * sdio_memcpy_fromio - read a chunk of memory from a SDIO function
  386. * @func: SDIO function to access
  387. * @dst: buffer to store the data
  388. * @addr: address to begin reading from
  389. * @count: number of bytes to read
  390. *
  391. * Reads from the address space of a given SDIO function. Return
  392. * value indicates if the transfer succeeded or not.
  393. */
  394. int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
  395. unsigned int addr, int count)
  396. {
  397. return sdio_io_rw_ext_helper(func, 0, addr, 1, dst, count);
  398. }
  399. EXPORT_SYMBOL_GPL(sdio_memcpy_fromio);
  400. /**
  401. * sdio_memcpy_toio - write a chunk of memory to a SDIO function
  402. * @func: SDIO function to access
  403. * @addr: address to start writing to
  404. * @src: buffer that contains the data to write
  405. * @count: number of bytes to write
  406. *
  407. * Writes to the address space of a given SDIO function. Return
  408. * value indicates if the transfer succeeded or not.
  409. */
  410. int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
  411. void *src, int count)
  412. {
  413. return sdio_io_rw_ext_helper(func, 1, addr, 1, src, count);
  414. }
  415. EXPORT_SYMBOL_GPL(sdio_memcpy_toio);
  416. /**
  417. * sdio_readsb - read from a FIFO on a SDIO function
  418. * @func: SDIO function to access
  419. * @dst: buffer to store the data
  420. * @addr: address of (single byte) FIFO
  421. * @count: number of bytes to read
  422. *
  423. * Reads from the specified FIFO of a given SDIO function. Return
  424. * value indicates if the transfer succeeded or not.
  425. */
  426. int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
  427. int count)
  428. {
  429. return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count);
  430. }
  431. EXPORT_SYMBOL_GPL(sdio_readsb);
  432. /**
  433. * sdio_writesb - write to a FIFO of a SDIO function
  434. * @func: SDIO function to access
  435. * @addr: address of (single byte) FIFO
  436. * @src: buffer that contains the data to write
  437. * @count: number of bytes to write
  438. *
  439. * Writes to the specified FIFO of a given SDIO function. Return
  440. * value indicates if the transfer succeeded or not.
  441. */
  442. int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src,
  443. int count)
  444. {
  445. return sdio_io_rw_ext_helper(func, 1, addr, 0, src, count);
  446. }
  447. EXPORT_SYMBOL_GPL(sdio_writesb);
  448. /**
  449. * sdio_readw - read a 16 bit integer from a SDIO function
  450. * @func: SDIO function to access
  451. * @addr: address to read
  452. * @err_ret: optional status value from transfer
  453. *
  454. * Reads a 16 bit integer from the address space of a given SDIO
  455. * function. If there is a problem reading the address, 0xffff
  456. * is returned and @err_ret will contain the error code.
  457. */
  458. u16 sdio_readw(struct sdio_func *func, unsigned int addr, int *err_ret)
  459. {
  460. int ret;
  461. ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2);
  462. if (err_ret)
  463. *err_ret = ret;
  464. if (ret)
  465. return 0xFFFF;
  466. return le16_to_cpup((__le16 *)func->tmpbuf);
  467. }
  468. EXPORT_SYMBOL_GPL(sdio_readw);
  469. /**
  470. * sdio_writew - write a 16 bit integer to a SDIO function
  471. * @func: SDIO function to access
  472. * @b: integer to write
  473. * @addr: address to write to
  474. * @err_ret: optional status value from transfer
  475. *
  476. * Writes a 16 bit integer to the address space of a given SDIO
  477. * function. @err_ret will contain the status of the actual
  478. * transfer.
  479. */
  480. void sdio_writew(struct sdio_func *func, u16 b, unsigned int addr, int *err_ret)
  481. {
  482. int ret;
  483. *(__le16 *)func->tmpbuf = cpu_to_le16(b);
  484. ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
  485. if (err_ret)
  486. *err_ret = ret;
  487. }
  488. EXPORT_SYMBOL_GPL(sdio_writew);
  489. /**
  490. * sdio_readl - read a 32 bit integer from a SDIO function
  491. * @func: SDIO function to access
  492. * @addr: address to read
  493. * @err_ret: optional status value from transfer
  494. *
  495. * Reads a 32 bit integer from the address space of a given SDIO
  496. * function. If there is a problem reading the address,
  497. * 0xffffffff is returned and @err_ret will contain the error
  498. * code.
  499. */
  500. u32 sdio_readl(struct sdio_func *func, unsigned int addr, int *err_ret)
  501. {
  502. int ret;
  503. ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4);
  504. if (err_ret)
  505. *err_ret = ret;
  506. if (ret)
  507. return 0xFFFFFFFF;
  508. return le32_to_cpup((__le32 *)func->tmpbuf);
  509. }
  510. EXPORT_SYMBOL_GPL(sdio_readl);
  511. /**
  512. * sdio_writel - write a 32 bit integer to a SDIO function
  513. * @func: SDIO function to access
  514. * @b: integer to write
  515. * @addr: address to write to
  516. * @err_ret: optional status value from transfer
  517. *
  518. * Writes a 32 bit integer to the address space of a given SDIO
  519. * function. @err_ret will contain the status of the actual
  520. * transfer.
  521. */
  522. void sdio_writel(struct sdio_func *func, u32 b, unsigned int addr, int *err_ret)
  523. {
  524. int ret;
  525. *(__le32 *)func->tmpbuf = cpu_to_le32(b);
  526. ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);
  527. if (err_ret)
  528. *err_ret = ret;
  529. }
  530. EXPORT_SYMBOL_GPL(sdio_writel);
  531. /**
  532. * sdio_f0_readb - read a single byte from SDIO function 0
  533. * @func: an SDIO function of the card
  534. * @addr: address to read
  535. * @err_ret: optional status value from transfer
  536. *
  537. * Reads a single byte from the address space of SDIO function 0.
  538. * If there is a problem reading the address, 0xff is returned
  539. * and @err_ret will contain the error code.
  540. */
  541. unsigned char sdio_f0_readb(struct sdio_func *func, unsigned int addr,
  542. int *err_ret)
  543. {
  544. int ret;
  545. unsigned char val;
  546. if (!func) {
  547. if (err_ret)
  548. *err_ret = -EINVAL;
  549. return 0xFF;
  550. }
  551. ret = mmc_io_rw_direct(func->card, 0, 0, addr, 0, &val);
  552. if (err_ret)
  553. *err_ret = ret;
  554. if (ret)
  555. return 0xFF;
  556. return val;
  557. }
  558. EXPORT_SYMBOL_GPL(sdio_f0_readb);
  559. /**
  560. * sdio_f0_writeb - write a single byte to SDIO function 0
  561. * @func: an SDIO function of the card
  562. * @b: byte to write
  563. * @addr: address to write to
  564. * @err_ret: optional status value from transfer
  565. *
  566. * Writes a single byte to the address space of SDIO function 0.
  567. * @err_ret will contain the status of the actual transfer.
  568. *
  569. * Only writes to the vendor specific CCCR registers (0xF0 -
  570. * 0xFF) are permiited; @err_ret will be set to -EINVAL for *
  571. * writes outside this range.
  572. */
  573. void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
  574. int *err_ret)
  575. {
  576. int ret;
  577. if (!func) {
  578. if (err_ret)
  579. *err_ret = -EINVAL;
  580. return;
  581. }
  582. if ((addr < 0xF0 || addr > 0xFF) && (!mmc_card_lenient_fn0(func->card))) {
  583. if (err_ret)
  584. *err_ret = -EINVAL;
  585. return;
  586. }
  587. ret = mmc_io_rw_direct(func->card, 1, 0, addr, b, NULL);
  588. if (err_ret)
  589. *err_ret = ret;
  590. }
  591. EXPORT_SYMBOL_GPL(sdio_f0_writeb);
  592. /**
  593. * sdio_get_host_pm_caps - get host power management capabilities
  594. * @func: SDIO function attached to host
  595. *
  596. * Returns a capability bitmask corresponding to power management
  597. * features supported by the host controller that the card function
  598. * might rely upon during a system suspend. The host doesn't need
  599. * to be claimed, nor the function active, for this information to be
  600. * obtained.
  601. */
  602. mmc_pm_flag_t sdio_get_host_pm_caps(struct sdio_func *func)
  603. {
  604. if (!func)
  605. return 0;
  606. return func->card->host->pm_caps;
  607. }
  608. EXPORT_SYMBOL_GPL(sdio_get_host_pm_caps);
  609. /**
  610. * sdio_set_host_pm_flags - set wanted host power management capabilities
  611. * @func: SDIO function attached to host
  612. *
  613. * Set a capability bitmask corresponding to wanted host controller
  614. * power management features for the upcoming suspend state.
  615. * This must be called, if needed, each time the suspend method of
  616. * the function driver is called, and must contain only bits that
  617. * were returned by sdio_get_host_pm_caps().
  618. * The host doesn't need to be claimed, nor the function active,
  619. * for this information to be set.
  620. */
  621. int sdio_set_host_pm_flags(struct sdio_func *func, mmc_pm_flag_t flags)
  622. {
  623. struct mmc_host *host;
  624. if (!func)
  625. return -EINVAL;
  626. host = func->card->host;
  627. if (flags & ~host->pm_caps)
  628. return -EINVAL;
  629. /* function suspend methods are serialized, hence no lock needed */
  630. host->pm_flags |= flags;
  631. return 0;
  632. }
  633. EXPORT_SYMBOL_GPL(sdio_set_host_pm_flags);
  634. /**
  635. * sdio_retune_crc_disable - temporarily disable retuning on CRC errors
  636. * @func: SDIO function attached to host
  637. *
  638. * If the SDIO card is known to be in a state where it might produce
  639. * CRC errors on the bus in response to commands (like if we know it is
  640. * transitioning between power states), an SDIO function driver can
  641. * call this function to temporarily disable the SD/MMC core behavior of
  642. * triggering an automatic retuning.
  643. *
  644. * This function should be called while the host is claimed and the host
  645. * should remain claimed until sdio_retune_crc_enable() is called.
  646. * Specifically, the expected sequence of calls is:
  647. * - sdio_claim_host()
  648. * - sdio_retune_crc_disable()
  649. * - some number of calls like sdio_writeb() and sdio_readb()
  650. * - sdio_retune_crc_enable()
  651. * - sdio_release_host()
  652. */
  653. void sdio_retune_crc_disable(struct sdio_func *func)
  654. {
  655. func->card->host->retune_crc_disable = true;
  656. }
  657. EXPORT_SYMBOL_GPL(sdio_retune_crc_disable);
  658. /**
  659. * sdio_retune_crc_enable - re-enable retuning on CRC errors
  660. * @func: SDIO function attached to host
  661. *
  662. * This is the compement to sdio_retune_crc_disable().
  663. */
  664. void sdio_retune_crc_enable(struct sdio_func *func)
  665. {
  666. func->card->host->retune_crc_disable = false;
  667. }
  668. EXPORT_SYMBOL_GPL(sdio_retune_crc_enable);
  669. /**
  670. * sdio_retune_hold_now - start deferring retuning requests till release
  671. * @func: SDIO function attached to host
  672. *
  673. * This function can be called if it's currently a bad time to do
  674. * a retune of the SDIO card. Retune requests made during this time
  675. * will be held and we'll actually do the retune sometime after the
  676. * release.
  677. *
  678. * This function could be useful if an SDIO card is in a power state
  679. * where it can respond to a small subset of commands that doesn't
  680. * include the retuning command. Care should be taken when using
  681. * this function since (presumably) the retuning request we might be
  682. * deferring was made for a good reason.
  683. *
  684. * This function should be called while the host is claimed.
  685. */
  686. void sdio_retune_hold_now(struct sdio_func *func)
  687. {
  688. mmc_retune_hold_now(func->card->host);
  689. }
  690. EXPORT_SYMBOL_GPL(sdio_retune_hold_now);
  691. /**
  692. * sdio_retune_release - signal that it's OK to retune now
  693. * @func: SDIO function attached to host
  694. *
  695. * This is the complement to sdio_retune_hold_now(). Calling this
  696. * function won't make a retune happen right away but will allow
  697. * them to be scheduled normally.
  698. *
  699. * This function should be called while the host is claimed.
  700. */
  701. void sdio_retune_release(struct sdio_func *func)
  702. {
  703. mmc_retune_release(func->card->host);
  704. }
  705. EXPORT_SYMBOL_GPL(sdio_retune_release);