sdio_io.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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_readb - read a single byte from a SDIO function
  118. * @func: SDIO function to access
  119. * @addr: address to read
  120. * @err_ret: optional status value from transfer
  121. *
  122. * Reads a single byte from the address space of a given SDIO
  123. * function. If there is a problem reading the address, 0xff
  124. * is returned and @err_ret will contain the error code.
  125. */
  126. unsigned char sdio_readb(struct sdio_func *func, unsigned int addr,
  127. int *err_ret)
  128. {
  129. int ret;
  130. unsigned char val;
  131. BUG_ON(!func);
  132. if (err_ret)
  133. *err_ret = 0;
  134. ret = mmc_io_rw_direct(func->card, 0, func->num, addr, 0, &val);
  135. if (ret) {
  136. if (err_ret)
  137. *err_ret = ret;
  138. return 0xFF;
  139. }
  140. return val;
  141. }
  142. EXPORT_SYMBOL_GPL(sdio_readb);
  143. /**
  144. * sdio_writeb - write a single byte to a SDIO function
  145. * @func: SDIO function to access
  146. * @b: byte to write
  147. * @addr: address to write to
  148. * @err_ret: optional status value from transfer
  149. *
  150. * Writes a single byte to the address space of a given SDIO
  151. * function. @err_ret will contain the status of the actual
  152. * transfer.
  153. */
  154. void sdio_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
  155. int *err_ret)
  156. {
  157. int ret;
  158. BUG_ON(!func);
  159. ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL);
  160. if (err_ret)
  161. *err_ret = ret;
  162. }
  163. EXPORT_SYMBOL_GPL(sdio_writeb);
  164. /**
  165. * sdio_memcpy_fromio - read a chunk of memory from a SDIO function
  166. * @func: SDIO function to access
  167. * @dst: buffer to store the data
  168. * @addr: address to begin reading from
  169. * @count: number of bytes to read
  170. *
  171. * Reads up to 512 bytes from the address space of a given SDIO
  172. * function. Return value indicates if the transfer succeeded or
  173. * not.
  174. */
  175. int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
  176. unsigned int addr, int count)
  177. {
  178. return mmc_io_rw_extended(func->card, 0, func->num, addr, 0, dst,
  179. count);
  180. }
  181. EXPORT_SYMBOL_GPL(sdio_memcpy_fromio);
  182. /**
  183. * sdio_memcpy_toio - write a chunk of memory to a SDIO function
  184. * @func: SDIO function to access
  185. * @addr: address to start writing to
  186. * @src: buffer that contains the data to write
  187. * @count: number of bytes to write
  188. *
  189. * Writes up to 512 bytes to the address space of a given SDIO
  190. * function. Return value indicates if the transfer succeeded or
  191. * not.
  192. */
  193. int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
  194. void *src, int count)
  195. {
  196. return mmc_io_rw_extended(func->card, 1, func->num, addr, 0, src,
  197. count);
  198. }
  199. EXPORT_SYMBOL_GPL(sdio_memcpy_toio);
  200. /**
  201. * sdio_readsb - read from a FIFO on a SDIO function
  202. * @func: SDIO function to access
  203. * @dst: buffer to store the data
  204. * @addr: address of (single byte) FIFO
  205. * @count: number of bytes to read
  206. *
  207. * Reads up to 512 bytes from the specified FIFO of a given SDIO
  208. * function. Return value indicates if the transfer succeeded or
  209. * not.
  210. */
  211. int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
  212. int count)
  213. {
  214. return mmc_io_rw_extended(func->card, 0, func->num, addr, 1, dst,
  215. count);
  216. }
  217. EXPORT_SYMBOL_GPL(sdio_readsb);
  218. /**
  219. * sdio_writesb - write to a FIFO of a SDIO function
  220. * @func: SDIO function to access
  221. * @addr: address of (single byte) FIFO
  222. * @src: buffer that contains the data to write
  223. * @count: number of bytes to write
  224. *
  225. * Writes up to 512 bytes to the specified FIFO of a given SDIO
  226. * function. Return value indicates if the transfer succeeded or
  227. * not.
  228. */
  229. int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src,
  230. int count)
  231. {
  232. return mmc_io_rw_extended(func->card, 1, func->num, addr, 1, src,
  233. count);
  234. }
  235. EXPORT_SYMBOL_GPL(sdio_writesb);
  236. /**
  237. * sdio_readw - read a 16 bit integer from a SDIO function
  238. * @func: SDIO function to access
  239. * @addr: address to read
  240. * @err_ret: optional status value from transfer
  241. *
  242. * Reads a 16 bit integer from the address space of a given SDIO
  243. * function. If there is a problem reading the address, 0xffff
  244. * is returned and @err_ret will contain the error code.
  245. */
  246. unsigned short sdio_readw(struct sdio_func *func, unsigned int addr,
  247. int *err_ret)
  248. {
  249. int ret;
  250. if (err_ret)
  251. *err_ret = 0;
  252. ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2);
  253. if (ret) {
  254. if (err_ret)
  255. *err_ret = ret;
  256. return 0xFFFF;
  257. }
  258. return le16_to_cpu(*(u16*)func->tmpbuf);
  259. }
  260. EXPORT_SYMBOL_GPL(sdio_readw);
  261. /**
  262. * sdio_writew - write a 16 bit integer to a SDIO function
  263. * @func: SDIO function to access
  264. * @b: integer to write
  265. * @addr: address to write to
  266. * @err_ret: optional status value from transfer
  267. *
  268. * Writes a 16 bit integer to the address space of a given SDIO
  269. * function. @err_ret will contain the status of the actual
  270. * transfer.
  271. */
  272. void sdio_writew(struct sdio_func *func, unsigned short b, unsigned int addr,
  273. int *err_ret)
  274. {
  275. int ret;
  276. *(u16*)func->tmpbuf = cpu_to_le16(b);
  277. ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
  278. if (err_ret)
  279. *err_ret = ret;
  280. }
  281. EXPORT_SYMBOL_GPL(sdio_writew);
  282. /**
  283. * sdio_readl - read a 32 bit integer from a SDIO function
  284. * @func: SDIO function to access
  285. * @addr: address to read
  286. * @err_ret: optional status value from transfer
  287. *
  288. * Reads a 32 bit integer from the address space of a given SDIO
  289. * function. If there is a problem reading the address,
  290. * 0xffffffff is returned and @err_ret will contain the error
  291. * code.
  292. */
  293. unsigned long sdio_readl(struct sdio_func *func, unsigned int addr,
  294. int *err_ret)
  295. {
  296. int ret;
  297. if (err_ret)
  298. *err_ret = 0;
  299. ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4);
  300. if (ret) {
  301. if (err_ret)
  302. *err_ret = ret;
  303. return 0xFFFFFFFF;
  304. }
  305. return le32_to_cpu(*(u32*)func->tmpbuf);
  306. }
  307. EXPORT_SYMBOL_GPL(sdio_readl);
  308. /**
  309. * sdio_writel - write a 32 bit integer to a SDIO function
  310. * @func: SDIO function to access
  311. * @b: integer to write
  312. * @addr: address to write to
  313. * @err_ret: optional status value from transfer
  314. *
  315. * Writes a 32 bit integer to the address space of a given SDIO
  316. * function. @err_ret will contain the status of the actual
  317. * transfer.
  318. */
  319. void sdio_writel(struct sdio_func *func, unsigned long b, unsigned int addr,
  320. int *err_ret)
  321. {
  322. int ret;
  323. *(u32*)func->tmpbuf = cpu_to_le32(b);
  324. ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);
  325. if (err_ret)
  326. *err_ret = ret;
  327. }
  328. EXPORT_SYMBOL_GPL(sdio_writel);