sdio_io.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * linux/drivers/mmc/core/sdio_io.c
  3. *
  4. * Copyright 2007 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/mmc/host.h>
  12. #include <linux/mmc/card.h>
  13. #include <linux/mmc/sdio.h>
  14. #include <linux/mmc/sdio_func.h>
  15. #include "sdio_ops.h"
  16. /**
  17. * sdio_claim_host - exclusively claim a bus for a certain SDIO function
  18. * @func: SDIO function that will be accessed
  19. *
  20. * Claim a bus for a set of operations. The SDIO function given
  21. * is used to figure out which bus is relevant.
  22. */
  23. void sdio_claim_host(struct sdio_func *func)
  24. {
  25. BUG_ON(!func);
  26. BUG_ON(!func->card);
  27. mmc_claim_host(func->card->host);
  28. }
  29. EXPORT_SYMBOL_GPL(sdio_claim_host);
  30. /**
  31. * sdio_release_host - release a bus for a certain SDIO function
  32. * @func: SDIO function that was accessed
  33. *
  34. * Release a bus, allowing others to claim the bus for their
  35. * operations.
  36. */
  37. void sdio_release_host(struct sdio_func *func)
  38. {
  39. BUG_ON(!func);
  40. BUG_ON(!func->card);
  41. mmc_release_host(func->card->host);
  42. }
  43. EXPORT_SYMBOL_GPL(sdio_release_host);
  44. /**
  45. * sdio_enable_func - enables a SDIO function for usage
  46. * @func: SDIO function to enable
  47. *
  48. * Powers up and activates a SDIO function so that register
  49. * access is possible.
  50. */
  51. int sdio_enable_func(struct sdio_func *func)
  52. {
  53. int ret;
  54. unsigned char reg;
  55. unsigned long timeout;
  56. BUG_ON(!func);
  57. BUG_ON(!func->card);
  58. pr_debug("SDIO: Enabling device %s...\n", sdio_func_id(func));
  59. ret = mmc_io_rw_direct(func->card, 0, 0, SDIO_CCCR_IOEx, 0, &reg);
  60. if (ret)
  61. goto err;
  62. reg |= 1 << func->num;
  63. ret = mmc_io_rw_direct(func->card, 1, 0, SDIO_CCCR_IOEx, reg, NULL);
  64. if (ret)
  65. goto err;
  66. /*
  67. * FIXME: This should timeout based on information in the CIS,
  68. * but we don't have card to parse that yet.
  69. */
  70. timeout = jiffies + HZ;
  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. BUG_ON(!func);
  100. BUG_ON(!func->card);
  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(min(
  142. func->max_blksize,
  143. func->card->host->max_blk_size),
  144. 512u);
  145. }
  146. ret = mmc_io_rw_direct(func->card, 1, 0,
  147. SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE,
  148. blksz & 0xff, NULL);
  149. if (ret)
  150. return ret;
  151. ret = mmc_io_rw_direct(func->card, 1, 0,
  152. SDIO_FBR_BASE(func->num) + SDIO_FBR_BLKSIZE + 1,
  153. (blksz >> 8) & 0xff, NULL);
  154. if (ret)
  155. return ret;
  156. func->cur_blksize = blksz;
  157. return 0;
  158. }
  159. EXPORT_SYMBOL_GPL(sdio_set_block_size);
  160. /**
  161. * sdio_readb - read a single byte from a SDIO function
  162. * @func: SDIO function to access
  163. * @addr: address to read
  164. * @err_ret: optional status value from transfer
  165. *
  166. * Reads a single byte from the address space of a given SDIO
  167. * function. If there is a problem reading the address, 0xff
  168. * is returned and @err_ret will contain the error code.
  169. */
  170. unsigned char sdio_readb(struct sdio_func *func, unsigned int addr,
  171. int *err_ret)
  172. {
  173. int ret;
  174. unsigned char val;
  175. BUG_ON(!func);
  176. if (err_ret)
  177. *err_ret = 0;
  178. ret = mmc_io_rw_direct(func->card, 0, func->num, addr, 0, &val);
  179. if (ret) {
  180. if (err_ret)
  181. *err_ret = ret;
  182. return 0xFF;
  183. }
  184. return val;
  185. }
  186. EXPORT_SYMBOL_GPL(sdio_readb);
  187. /**
  188. * sdio_writeb - write a single byte to a SDIO function
  189. * @func: SDIO function to access
  190. * @b: byte to write
  191. * @addr: address to write to
  192. * @err_ret: optional status value from transfer
  193. *
  194. * Writes a single byte to the address space of a given SDIO
  195. * function. @err_ret will contain the status of the actual
  196. * transfer.
  197. */
  198. void sdio_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
  199. int *err_ret)
  200. {
  201. int ret;
  202. BUG_ON(!func);
  203. ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL);
  204. if (err_ret)
  205. *err_ret = ret;
  206. }
  207. EXPORT_SYMBOL_GPL(sdio_writeb);
  208. /**
  209. * sdio_memcpy_fromio - read a chunk of memory from a SDIO function
  210. * @func: SDIO function to access
  211. * @dst: buffer to store the data
  212. * @addr: address to begin reading from
  213. * @count: number of bytes to read
  214. *
  215. * Reads up to 512 bytes from the address space of a given SDIO
  216. * function. Return value indicates if the transfer succeeded or
  217. * not.
  218. */
  219. int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
  220. unsigned int addr, int count)
  221. {
  222. return mmc_io_rw_extended(func->card, 0, func->num, addr, 0, dst,
  223. count);
  224. }
  225. EXPORT_SYMBOL_GPL(sdio_memcpy_fromio);
  226. /**
  227. * sdio_memcpy_toio - write a chunk of memory to a SDIO function
  228. * @func: SDIO function to access
  229. * @addr: address to start writing to
  230. * @src: buffer that contains the data to write
  231. * @count: number of bytes to write
  232. *
  233. * Writes up to 512 bytes to the address space of a given SDIO
  234. * function. Return value indicates if the transfer succeeded or
  235. * not.
  236. */
  237. int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
  238. void *src, int count)
  239. {
  240. return mmc_io_rw_extended(func->card, 1, func->num, addr, 0, src,
  241. count);
  242. }
  243. EXPORT_SYMBOL_GPL(sdio_memcpy_toio);
  244. /**
  245. * sdio_readsb - read from a FIFO on a SDIO function
  246. * @func: SDIO function to access
  247. * @dst: buffer to store the data
  248. * @addr: address of (single byte) FIFO
  249. * @count: number of bytes to read
  250. *
  251. * Reads up to 512 bytes from the specified FIFO of a given SDIO
  252. * function. Return value indicates if the transfer succeeded or
  253. * not.
  254. */
  255. int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
  256. int count)
  257. {
  258. return mmc_io_rw_extended(func->card, 0, func->num, addr, 1, dst,
  259. count);
  260. }
  261. EXPORT_SYMBOL_GPL(sdio_readsb);
  262. /**
  263. * sdio_writesb - write to a FIFO of a SDIO function
  264. * @func: SDIO function to access
  265. * @addr: address of (single byte) FIFO
  266. * @src: buffer that contains the data to write
  267. * @count: number of bytes to write
  268. *
  269. * Writes up to 512 bytes to the specified FIFO of a given SDIO
  270. * function. Return value indicates if the transfer succeeded or
  271. * not.
  272. */
  273. int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src,
  274. int count)
  275. {
  276. return mmc_io_rw_extended(func->card, 1, func->num, addr, 1, src,
  277. count);
  278. }
  279. EXPORT_SYMBOL_GPL(sdio_writesb);
  280. /**
  281. * sdio_readw - read a 16 bit integer from a SDIO function
  282. * @func: SDIO function to access
  283. * @addr: address to read
  284. * @err_ret: optional status value from transfer
  285. *
  286. * Reads a 16 bit integer from the address space of a given SDIO
  287. * function. If there is a problem reading the address, 0xffff
  288. * is returned and @err_ret will contain the error code.
  289. */
  290. unsigned short sdio_readw(struct sdio_func *func, unsigned int addr,
  291. int *err_ret)
  292. {
  293. int ret;
  294. if (err_ret)
  295. *err_ret = 0;
  296. ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2);
  297. if (ret) {
  298. if (err_ret)
  299. *err_ret = ret;
  300. return 0xFFFF;
  301. }
  302. return le16_to_cpu(*(u16*)func->tmpbuf);
  303. }
  304. EXPORT_SYMBOL_GPL(sdio_readw);
  305. /**
  306. * sdio_writew - write a 16 bit integer to a SDIO function
  307. * @func: SDIO function to access
  308. * @b: integer to write
  309. * @addr: address to write to
  310. * @err_ret: optional status value from transfer
  311. *
  312. * Writes a 16 bit integer to the address space of a given SDIO
  313. * function. @err_ret will contain the status of the actual
  314. * transfer.
  315. */
  316. void sdio_writew(struct sdio_func *func, unsigned short b, unsigned int addr,
  317. int *err_ret)
  318. {
  319. int ret;
  320. *(u16*)func->tmpbuf = cpu_to_le16(b);
  321. ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
  322. if (err_ret)
  323. *err_ret = ret;
  324. }
  325. EXPORT_SYMBOL_GPL(sdio_writew);
  326. /**
  327. * sdio_readl - read a 32 bit integer from a SDIO function
  328. * @func: SDIO function to access
  329. * @addr: address to read
  330. * @err_ret: optional status value from transfer
  331. *
  332. * Reads a 32 bit integer from the address space of a given SDIO
  333. * function. If there is a problem reading the address,
  334. * 0xffffffff is returned and @err_ret will contain the error
  335. * code.
  336. */
  337. unsigned long sdio_readl(struct sdio_func *func, unsigned int addr,
  338. int *err_ret)
  339. {
  340. int ret;
  341. if (err_ret)
  342. *err_ret = 0;
  343. ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4);
  344. if (ret) {
  345. if (err_ret)
  346. *err_ret = ret;
  347. return 0xFFFFFFFF;
  348. }
  349. return le32_to_cpu(*(u32*)func->tmpbuf);
  350. }
  351. EXPORT_SYMBOL_GPL(sdio_readl);
  352. /**
  353. * sdio_writel - write a 32 bit integer to a SDIO function
  354. * @func: SDIO function to access
  355. * @b: integer to write
  356. * @addr: address to write to
  357. * @err_ret: optional status value from transfer
  358. *
  359. * Writes a 32 bit integer to the address space of a given SDIO
  360. * function. @err_ret will contain the status of the actual
  361. * transfer.
  362. */
  363. void sdio_writel(struct sdio_func *func, unsigned long b, unsigned int addr,
  364. int *err_ret)
  365. {
  366. int ret;
  367. *(u32*)func->tmpbuf = cpu_to_le32(b);
  368. ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);
  369. if (err_ret)
  370. *err_ret = ret;
  371. }
  372. EXPORT_SYMBOL_GPL(sdio_writel);