sdio_io.c 21 KB

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