sdio_io.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  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. /**
  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. BUG_ON(!func);
  27. BUG_ON(!func->card);
  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. BUG_ON(!func);
  41. BUG_ON(!func->card);
  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. BUG_ON(!func);
  58. BUG_ON(!func->card);
  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. BUG_ON(!func);
  97. BUG_ON(!func->card);
  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 -EIO;
  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 = min(func->card->host->max_seg_size,
  161. func->card->host->max_blk_size);
  162. if (mmc_blksz_for_byte_mode(func->card))
  163. mval = min(mval, func->cur_blksize);
  164. else
  165. mval = min(mval, func->max_blksize);
  166. if (mmc_card_broken_byte_mode_512(func->card))
  167. return min(mval, 511u);
  168. return min(mval, 512u); /* maximum size for byte mode */
  169. }
  170. /**
  171. * sdio_align_size - pads a transfer size to a more optimal value
  172. * @func: SDIO function
  173. * @sz: original transfer size
  174. *
  175. * Pads the original data size with a number of extra bytes in
  176. * order to avoid controller bugs and/or performance hits
  177. * (e.g. some controllers revert to PIO for certain sizes).
  178. *
  179. * If possible, it will also adjust the size so that it can be
  180. * handled in just a single request.
  181. *
  182. * Returns the improved size, which might be unmodified.
  183. */
  184. unsigned int sdio_align_size(struct sdio_func *func, unsigned int sz)
  185. {
  186. unsigned int orig_sz;
  187. unsigned int blk_sz, byte_sz;
  188. unsigned chunk_sz;
  189. orig_sz = sz;
  190. /*
  191. * Do a first check with the controller, in case it
  192. * wants to increase the size up to a point where it
  193. * might need more than one block.
  194. */
  195. sz = mmc_align_data_size(func->card, sz);
  196. /*
  197. * If we can still do this with just a byte transfer, then
  198. * we're done.
  199. */
  200. if (sz <= sdio_max_byte_size(func))
  201. return sz;
  202. if (func->card->cccr.multi_block) {
  203. /*
  204. * Check if the transfer is already block aligned
  205. */
  206. if ((sz % func->cur_blksize) == 0)
  207. return sz;
  208. /*
  209. * Realign it so that it can be done with one request,
  210. * and recheck if the controller still likes it.
  211. */
  212. blk_sz = ((sz + func->cur_blksize - 1) /
  213. func->cur_blksize) * func->cur_blksize;
  214. blk_sz = mmc_align_data_size(func->card, blk_sz);
  215. /*
  216. * This value is only good if it is still just
  217. * one request.
  218. */
  219. if ((blk_sz % func->cur_blksize) == 0)
  220. return blk_sz;
  221. /*
  222. * We failed to do one request, but at least try to
  223. * pad the remainder properly.
  224. */
  225. byte_sz = mmc_align_data_size(func->card,
  226. sz % func->cur_blksize);
  227. if (byte_sz <= sdio_max_byte_size(func)) {
  228. blk_sz = sz / func->cur_blksize;
  229. return blk_sz * func->cur_blksize + byte_sz;
  230. }
  231. } else {
  232. /*
  233. * We need multiple requests, so first check that the
  234. * controller can handle the chunk size;
  235. */
  236. chunk_sz = mmc_align_data_size(func->card,
  237. sdio_max_byte_size(func));
  238. if (chunk_sz == sdio_max_byte_size(func)) {
  239. /*
  240. * Fix up the size of the remainder (if any)
  241. */
  242. byte_sz = orig_sz % chunk_sz;
  243. if (byte_sz) {
  244. byte_sz = mmc_align_data_size(func->card,
  245. byte_sz);
  246. }
  247. return (orig_sz / chunk_sz) * chunk_sz + byte_sz;
  248. }
  249. }
  250. /*
  251. * The controller is simply incapable of transferring the size
  252. * we want in decent manner, so just return the original size.
  253. */
  254. return orig_sz;
  255. }
  256. EXPORT_SYMBOL_GPL(sdio_align_size);
  257. /* Split an arbitrarily sized data transfer into several
  258. * IO_RW_EXTENDED commands. */
  259. static int sdio_io_rw_ext_helper(struct sdio_func *func, int write,
  260. unsigned addr, int incr_addr, u8 *buf, unsigned size)
  261. {
  262. unsigned remainder = size;
  263. unsigned max_blocks;
  264. int ret;
  265. /* Do the bulk of the transfer using block mode (if supported). */
  266. if (func->card->cccr.multi_block && (size > sdio_max_byte_size(func))) {
  267. /* Blocks per command is limited by host count, host transfer
  268. * size (we only use a single sg entry) and the maximum for
  269. * IO_RW_EXTENDED of 511 blocks. */
  270. max_blocks = min(func->card->host->max_blk_count,
  271. func->card->host->max_seg_size / func->cur_blksize);
  272. max_blocks = min(max_blocks, 511u);
  273. while (remainder >= func->cur_blksize) {
  274. unsigned blocks;
  275. blocks = remainder / func->cur_blksize;
  276. if (blocks > max_blocks)
  277. blocks = max_blocks;
  278. size = blocks * func->cur_blksize;
  279. ret = mmc_io_rw_extended(func->card, write,
  280. func->num, addr, incr_addr, buf,
  281. blocks, func->cur_blksize);
  282. if (ret)
  283. return ret;
  284. remainder -= size;
  285. buf += size;
  286. if (incr_addr)
  287. addr += size;
  288. }
  289. }
  290. /* Write the remainder using byte mode. */
  291. while (remainder > 0) {
  292. size = min(remainder, sdio_max_byte_size(func));
  293. /* Indicate byte mode by setting "blocks" = 0 */
  294. ret = mmc_io_rw_extended(func->card, write, func->num, addr,
  295. incr_addr, buf, 0, size);
  296. if (ret)
  297. return ret;
  298. remainder -= size;
  299. buf += size;
  300. if (incr_addr)
  301. addr += size;
  302. }
  303. return 0;
  304. }
  305. /**
  306. * sdio_readb - read a single byte from a SDIO function
  307. * @func: SDIO function to access
  308. * @addr: address to read
  309. * @err_ret: optional status value from transfer
  310. *
  311. * Reads a single byte from the address space of a given SDIO
  312. * function. If there is a problem reading the address, 0xff
  313. * is returned and @err_ret will contain the error code.
  314. */
  315. u8 sdio_readb(struct sdio_func *func, unsigned int addr, int *err_ret)
  316. {
  317. int ret;
  318. u8 val;
  319. BUG_ON(!func);
  320. if (err_ret)
  321. *err_ret = 0;
  322. ret = mmc_io_rw_direct(func->card, 0, func->num, addr, 0, &val);
  323. if (ret) {
  324. if (err_ret)
  325. *err_ret = ret;
  326. return 0xFF;
  327. }
  328. return val;
  329. }
  330. EXPORT_SYMBOL_GPL(sdio_readb);
  331. /**
  332. * sdio_readb_ext - read a single byte from a SDIO function
  333. * @func: SDIO function to access
  334. * @addr: address to read
  335. * @err_ret: optional status value from transfer
  336. * @in: value to add to argument
  337. *
  338. * Reads a single byte from the address space of a given SDIO
  339. * function. If there is a problem reading the address, 0xff
  340. * is returned and @err_ret will contain the error code.
  341. */
  342. unsigned char sdio_readb_ext(struct sdio_func *func, unsigned int addr,
  343. int *err_ret, unsigned in)
  344. {
  345. int ret;
  346. unsigned char val;
  347. BUG_ON(!func);
  348. if (err_ret)
  349. *err_ret = 0;
  350. ret = mmc_io_rw_direct(func->card, 0, func->num, addr, (u8)in, &val);
  351. if (ret) {
  352. if (err_ret)
  353. *err_ret = ret;
  354. return 0xFF;
  355. }
  356. return val;
  357. }
  358. EXPORT_SYMBOL_GPL(sdio_readb_ext);
  359. /**
  360. * sdio_writeb - write a single byte to a SDIO function
  361. * @func: SDIO function to access
  362. * @b: byte to write
  363. * @addr: address to write to
  364. * @err_ret: optional status value from transfer
  365. *
  366. * Writes a single byte to the address space of a given SDIO
  367. * function. @err_ret will contain the status of the actual
  368. * transfer.
  369. */
  370. void sdio_writeb(struct sdio_func *func, u8 b, unsigned int addr, int *err_ret)
  371. {
  372. int ret;
  373. BUG_ON(!func);
  374. ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL);
  375. if (err_ret)
  376. *err_ret = ret;
  377. }
  378. EXPORT_SYMBOL_GPL(sdio_writeb);
  379. /**
  380. * sdio_writeb_readb - write and read a byte from SDIO function
  381. * @func: SDIO function to access
  382. * @write_byte: byte to write
  383. * @addr: address to write to
  384. * @err_ret: optional status value from transfer
  385. *
  386. * Performs a RAW (Read after Write) operation as defined by SDIO spec -
  387. * single byte is written to address space of a given SDIO function and
  388. * response is read back from the same address, both using single request.
  389. * If there is a problem with the operation, 0xff is returned and
  390. * @err_ret will contain the error code.
  391. */
  392. u8 sdio_writeb_readb(struct sdio_func *func, u8 write_byte,
  393. unsigned int addr, int *err_ret)
  394. {
  395. int ret;
  396. u8 val;
  397. ret = mmc_io_rw_direct(func->card, 1, func->num, addr,
  398. write_byte, &val);
  399. if (err_ret)
  400. *err_ret = ret;
  401. if (ret)
  402. val = 0xff;
  403. return val;
  404. }
  405. EXPORT_SYMBOL_GPL(sdio_writeb_readb);
  406. /**
  407. * sdio_memcpy_fromio - read a chunk of memory from a SDIO function
  408. * @func: SDIO function to access
  409. * @dst: buffer to store the data
  410. * @addr: address to begin reading from
  411. * @count: number of bytes to read
  412. *
  413. * Reads from the address space of a given SDIO function. Return
  414. * value indicates if the transfer succeeded or not.
  415. */
  416. int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
  417. unsigned int addr, int count)
  418. {
  419. return sdio_io_rw_ext_helper(func, 0, addr, 1, dst, count);
  420. }
  421. EXPORT_SYMBOL_GPL(sdio_memcpy_fromio);
  422. /**
  423. * sdio_memcpy_toio - write a chunk of memory to a SDIO function
  424. * @func: SDIO function to access
  425. * @addr: address to start writing to
  426. * @src: buffer that contains the data to write
  427. * @count: number of bytes to write
  428. *
  429. * Writes to the address space of a given SDIO function. Return
  430. * value indicates if the transfer succeeded or not.
  431. */
  432. int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
  433. void *src, int count)
  434. {
  435. return sdio_io_rw_ext_helper(func, 1, addr, 1, src, count);
  436. }
  437. EXPORT_SYMBOL_GPL(sdio_memcpy_toio);
  438. /**
  439. * sdio_readsb - read from a FIFO on a SDIO function
  440. * @func: SDIO function to access
  441. * @dst: buffer to store the data
  442. * @addr: address of (single byte) FIFO
  443. * @count: number of bytes to read
  444. *
  445. * Reads from the specified FIFO of a given SDIO function. Return
  446. * value indicates if the transfer succeeded or not.
  447. */
  448. int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
  449. int count)
  450. {
  451. return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count);
  452. }
  453. EXPORT_SYMBOL_GPL(sdio_readsb);
  454. /**
  455. * sdio_writesb - write to a FIFO of a SDIO function
  456. * @func: SDIO function to access
  457. * @addr: address of (single byte) FIFO
  458. * @src: buffer that contains the data to write
  459. * @count: number of bytes to write
  460. *
  461. * Writes to the specified FIFO of a given SDIO function. Return
  462. * value indicates if the transfer succeeded or not.
  463. */
  464. int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src,
  465. int count)
  466. {
  467. return sdio_io_rw_ext_helper(func, 1, addr, 0, src, count);
  468. }
  469. EXPORT_SYMBOL_GPL(sdio_writesb);
  470. /**
  471. * sdio_readw - read a 16 bit integer from a SDIO function
  472. * @func: SDIO function to access
  473. * @addr: address to read
  474. * @err_ret: optional status value from transfer
  475. *
  476. * Reads a 16 bit integer from the address space of a given SDIO
  477. * function. If there is a problem reading the address, 0xffff
  478. * is returned and @err_ret will contain the error code.
  479. */
  480. u16 sdio_readw(struct sdio_func *func, unsigned int addr, int *err_ret)
  481. {
  482. int ret;
  483. if (err_ret)
  484. *err_ret = 0;
  485. ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2);
  486. if (ret) {
  487. if (err_ret)
  488. *err_ret = ret;
  489. return 0xFFFF;
  490. }
  491. return le16_to_cpup((__le16 *)func->tmpbuf);
  492. }
  493. EXPORT_SYMBOL_GPL(sdio_readw);
  494. /**
  495. * sdio_writew - write a 16 bit integer to a SDIO function
  496. * @func: SDIO function to access
  497. * @b: integer to write
  498. * @addr: address to write to
  499. * @err_ret: optional status value from transfer
  500. *
  501. * Writes a 16 bit integer to the address space of a given SDIO
  502. * function. @err_ret will contain the status of the actual
  503. * transfer.
  504. */
  505. void sdio_writew(struct sdio_func *func, u16 b, unsigned int addr, int *err_ret)
  506. {
  507. int ret;
  508. *(__le16 *)func->tmpbuf = cpu_to_le16(b);
  509. ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
  510. if (err_ret)
  511. *err_ret = ret;
  512. }
  513. EXPORT_SYMBOL_GPL(sdio_writew);
  514. /**
  515. * sdio_readl - read a 32 bit integer from a SDIO function
  516. * @func: SDIO function to access
  517. * @addr: address to read
  518. * @err_ret: optional status value from transfer
  519. *
  520. * Reads a 32 bit integer from the address space of a given SDIO
  521. * function. If there is a problem reading the address,
  522. * 0xffffffff is returned and @err_ret will contain the error
  523. * code.
  524. */
  525. u32 sdio_readl(struct sdio_func *func, unsigned int addr, int *err_ret)
  526. {
  527. int ret;
  528. if (err_ret)
  529. *err_ret = 0;
  530. ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4);
  531. if (ret) {
  532. if (err_ret)
  533. *err_ret = ret;
  534. return 0xFFFFFFFF;
  535. }
  536. return le32_to_cpup((__le32 *)func->tmpbuf);
  537. }
  538. EXPORT_SYMBOL_GPL(sdio_readl);
  539. /**
  540. * sdio_writel - write a 32 bit integer to a SDIO function
  541. * @func: SDIO function to access
  542. * @b: integer to write
  543. * @addr: address to write to
  544. * @err_ret: optional status value from transfer
  545. *
  546. * Writes a 32 bit integer to the address space of a given SDIO
  547. * function. @err_ret will contain the status of the actual
  548. * transfer.
  549. */
  550. void sdio_writel(struct sdio_func *func, u32 b, unsigned int addr, int *err_ret)
  551. {
  552. int ret;
  553. *(__le32 *)func->tmpbuf = cpu_to_le32(b);
  554. ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);
  555. if (err_ret)
  556. *err_ret = ret;
  557. }
  558. EXPORT_SYMBOL_GPL(sdio_writel);
  559. /**
  560. * sdio_f0_readb - read a single byte from SDIO function 0
  561. * @func: an SDIO function of the card
  562. * @addr: address to read
  563. * @err_ret: optional status value from transfer
  564. *
  565. * Reads a single byte from the address space of SDIO function 0.
  566. * If there is a problem reading the address, 0xff is returned
  567. * and @err_ret will contain the error code.
  568. */
  569. unsigned char sdio_f0_readb(struct sdio_func *func, unsigned int addr,
  570. int *err_ret)
  571. {
  572. int ret;
  573. unsigned char val;
  574. BUG_ON(!func);
  575. if (err_ret)
  576. *err_ret = 0;
  577. ret = mmc_io_rw_direct(func->card, 0, 0, addr, 0, &val);
  578. if (ret) {
  579. if (err_ret)
  580. *err_ret = ret;
  581. return 0xFF;
  582. }
  583. return val;
  584. }
  585. EXPORT_SYMBOL_GPL(sdio_f0_readb);
  586. /**
  587. * sdio_f0_writeb - write a single byte to SDIO function 0
  588. * @func: an SDIO function of the card
  589. * @b: byte to write
  590. * @addr: address to write to
  591. * @err_ret: optional status value from transfer
  592. *
  593. * Writes a single byte to the address space of SDIO function 0.
  594. * @err_ret will contain the status of the actual transfer.
  595. *
  596. * Only writes to the vendor specific CCCR registers (0xF0 -
  597. * 0xFF) are permiited; @err_ret will be set to -EINVAL for *
  598. * writes outside this range.
  599. */
  600. void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
  601. int *err_ret)
  602. {
  603. int ret;
  604. BUG_ON(!func);
  605. if ((addr < 0xF0 || addr > 0xFF) && (!mmc_card_lenient_fn0(func->card))) {
  606. if (err_ret)
  607. *err_ret = -EINVAL;
  608. return;
  609. }
  610. ret = mmc_io_rw_direct(func->card, 1, 0, addr, b, NULL);
  611. if (err_ret)
  612. *err_ret = ret;
  613. }
  614. EXPORT_SYMBOL_GPL(sdio_f0_writeb);
  615. /**
  616. * sdio_get_host_pm_caps - get host power management capabilities
  617. * @func: SDIO function attached to host
  618. *
  619. * Returns a capability bitmask corresponding to power management
  620. * features supported by the host controller that the card function
  621. * might rely upon during a system suspend. The host doesn't need
  622. * to be claimed, nor the function active, for this information to be
  623. * obtained.
  624. */
  625. mmc_pm_flag_t sdio_get_host_pm_caps(struct sdio_func *func)
  626. {
  627. BUG_ON(!func);
  628. BUG_ON(!func->card);
  629. return func->card->host->pm_caps;
  630. }
  631. EXPORT_SYMBOL_GPL(sdio_get_host_pm_caps);
  632. /**
  633. * sdio_set_host_pm_flags - set wanted host power management capabilities
  634. * @func: SDIO function attached to host
  635. *
  636. * Set a capability bitmask corresponding to wanted host controller
  637. * power management features for the upcoming suspend state.
  638. * This must be called, if needed, each time the suspend method of
  639. * the function driver is called, and must contain only bits that
  640. * were returned by sdio_get_host_pm_caps().
  641. * The host doesn't need to be claimed, nor the function active,
  642. * for this information to be set.
  643. */
  644. int sdio_set_host_pm_flags(struct sdio_func *func, mmc_pm_flag_t flags)
  645. {
  646. struct mmc_host *host;
  647. BUG_ON(!func);
  648. BUG_ON(!func->card);
  649. host = func->card->host;
  650. if (flags & ~host->pm_caps)
  651. return -EINVAL;
  652. /* function suspend methods are serialized, hence no lock needed */
  653. host->pm_flags |= flags;
  654. return 0;
  655. }
  656. EXPORT_SYMBOL_GPL(sdio_set_host_pm_flags);