sdio_io.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. /* Split an arbitrarily sized data transfer into several
  161. * IO_RW_EXTENDED commands. */
  162. static int sdio_io_rw_ext_helper(struct sdio_func *func, int write,
  163. unsigned addr, int incr_addr, u8 *buf, unsigned size)
  164. {
  165. unsigned remainder = size;
  166. unsigned max_blocks;
  167. int ret;
  168. /* Do the bulk of the transfer using block mode (if supported). */
  169. if (func->card->cccr.multi_block) {
  170. /* Blocks per command is limited by host count, host transfer
  171. * size (we only use a single sg entry) and the maximum for
  172. * IO_RW_EXTENDED of 511 blocks. */
  173. max_blocks = min(min(
  174. func->card->host->max_blk_count,
  175. func->card->host->max_seg_size / func->cur_blksize),
  176. 511u);
  177. while (remainder > func->cur_blksize) {
  178. unsigned blocks;
  179. blocks = remainder / func->cur_blksize;
  180. if (blocks > max_blocks)
  181. blocks = max_blocks;
  182. size = blocks * func->cur_blksize;
  183. ret = mmc_io_rw_extended(func->card, write,
  184. func->num, addr, incr_addr, buf,
  185. blocks, func->cur_blksize);
  186. if (ret)
  187. return ret;
  188. remainder -= size;
  189. buf += size;
  190. if (incr_addr)
  191. addr += size;
  192. }
  193. }
  194. /* Write the remainder using byte mode. */
  195. while (remainder > 0) {
  196. size = remainder;
  197. if (size > func->cur_blksize)
  198. size = func->cur_blksize;
  199. if (size > 512)
  200. size = 512; /* maximum size for byte mode */
  201. ret = mmc_io_rw_extended(func->card, write, func->num, addr,
  202. incr_addr, buf, 1, size);
  203. if (ret)
  204. return ret;
  205. remainder -= size;
  206. buf += size;
  207. if (incr_addr)
  208. addr += size;
  209. }
  210. return 0;
  211. }
  212. /**
  213. * sdio_readb - read a single byte from a SDIO function
  214. * @func: SDIO function to access
  215. * @addr: address to read
  216. * @err_ret: optional status value from transfer
  217. *
  218. * Reads a single byte from the address space of a given SDIO
  219. * function. If there is a problem reading the address, 0xff
  220. * is returned and @err_ret will contain the error code.
  221. */
  222. unsigned char sdio_readb(struct sdio_func *func, unsigned int addr,
  223. int *err_ret)
  224. {
  225. int ret;
  226. unsigned char val;
  227. BUG_ON(!func);
  228. if (err_ret)
  229. *err_ret = 0;
  230. ret = mmc_io_rw_direct(func->card, 0, func->num, addr, 0, &val);
  231. if (ret) {
  232. if (err_ret)
  233. *err_ret = ret;
  234. return 0xFF;
  235. }
  236. return val;
  237. }
  238. EXPORT_SYMBOL_GPL(sdio_readb);
  239. /**
  240. * sdio_writeb - write a single byte to a SDIO function
  241. * @func: SDIO function to access
  242. * @b: byte to write
  243. * @addr: address to write to
  244. * @err_ret: optional status value from transfer
  245. *
  246. * Writes a single byte to the address space of a given SDIO
  247. * function. @err_ret will contain the status of the actual
  248. * transfer.
  249. */
  250. void sdio_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
  251. int *err_ret)
  252. {
  253. int ret;
  254. BUG_ON(!func);
  255. ret = mmc_io_rw_direct(func->card, 1, func->num, addr, b, NULL);
  256. if (err_ret)
  257. *err_ret = ret;
  258. }
  259. EXPORT_SYMBOL_GPL(sdio_writeb);
  260. /**
  261. * sdio_memcpy_fromio - read a chunk of memory from a SDIO function
  262. * @func: SDIO function to access
  263. * @dst: buffer to store the data
  264. * @addr: address to begin reading from
  265. * @count: number of bytes to read
  266. *
  267. * Reads from the address space of a given SDIO function. Return
  268. * value indicates if the transfer succeeded or not.
  269. */
  270. int sdio_memcpy_fromio(struct sdio_func *func, void *dst,
  271. unsigned int addr, int count)
  272. {
  273. return sdio_io_rw_ext_helper(func, 0, addr, 1, dst, count);
  274. }
  275. EXPORT_SYMBOL_GPL(sdio_memcpy_fromio);
  276. /**
  277. * sdio_memcpy_toio - write a chunk of memory to a SDIO function
  278. * @func: SDIO function to access
  279. * @addr: address to start writing to
  280. * @src: buffer that contains the data to write
  281. * @count: number of bytes to write
  282. *
  283. * Writes to the address space of a given SDIO function. Return
  284. * value indicates if the transfer succeeded or not.
  285. */
  286. int sdio_memcpy_toio(struct sdio_func *func, unsigned int addr,
  287. void *src, int count)
  288. {
  289. return sdio_io_rw_ext_helper(func, 1, addr, 1, src, count);
  290. }
  291. EXPORT_SYMBOL_GPL(sdio_memcpy_toio);
  292. /**
  293. * sdio_readsb - read from a FIFO on a SDIO function
  294. * @func: SDIO function to access
  295. * @dst: buffer to store the data
  296. * @addr: address of (single byte) FIFO
  297. * @count: number of bytes to read
  298. *
  299. * Reads from the specified FIFO of a given SDIO function. Return
  300. * value indicates if the transfer succeeded or not.
  301. */
  302. int sdio_readsb(struct sdio_func *func, void *dst, unsigned int addr,
  303. int count)
  304. {
  305. return sdio_io_rw_ext_helper(func, 0, addr, 0, dst, count);
  306. }
  307. EXPORT_SYMBOL_GPL(sdio_readsb);
  308. /**
  309. * sdio_writesb - write to a FIFO of a SDIO function
  310. * @func: SDIO function to access
  311. * @addr: address of (single byte) FIFO
  312. * @src: buffer that contains the data to write
  313. * @count: number of bytes to write
  314. *
  315. * Writes to the specified FIFO of a given SDIO function. Return
  316. * value indicates if the transfer succeeded or not.
  317. */
  318. int sdio_writesb(struct sdio_func *func, unsigned int addr, void *src,
  319. int count)
  320. {
  321. return sdio_io_rw_ext_helper(func, 1, addr, 0, src, count);
  322. }
  323. EXPORT_SYMBOL_GPL(sdio_writesb);
  324. /**
  325. * sdio_readw - read a 16 bit integer from a SDIO function
  326. * @func: SDIO function to access
  327. * @addr: address to read
  328. * @err_ret: optional status value from transfer
  329. *
  330. * Reads a 16 bit integer from the address space of a given SDIO
  331. * function. If there is a problem reading the address, 0xffff
  332. * is returned and @err_ret will contain the error code.
  333. */
  334. unsigned short sdio_readw(struct sdio_func *func, unsigned int addr,
  335. int *err_ret)
  336. {
  337. int ret;
  338. if (err_ret)
  339. *err_ret = 0;
  340. ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 2);
  341. if (ret) {
  342. if (err_ret)
  343. *err_ret = ret;
  344. return 0xFFFF;
  345. }
  346. return le16_to_cpu(*(u16*)func->tmpbuf);
  347. }
  348. EXPORT_SYMBOL_GPL(sdio_readw);
  349. /**
  350. * sdio_writew - write a 16 bit integer to a SDIO function
  351. * @func: SDIO function to access
  352. * @b: integer to write
  353. * @addr: address to write to
  354. * @err_ret: optional status value from transfer
  355. *
  356. * Writes a 16 bit integer to the address space of a given SDIO
  357. * function. @err_ret will contain the status of the actual
  358. * transfer.
  359. */
  360. void sdio_writew(struct sdio_func *func, unsigned short b, unsigned int addr,
  361. int *err_ret)
  362. {
  363. int ret;
  364. *(u16*)func->tmpbuf = cpu_to_le16(b);
  365. ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 2);
  366. if (err_ret)
  367. *err_ret = ret;
  368. }
  369. EXPORT_SYMBOL_GPL(sdio_writew);
  370. /**
  371. * sdio_readl - read a 32 bit integer from a SDIO function
  372. * @func: SDIO function to access
  373. * @addr: address to read
  374. * @err_ret: optional status value from transfer
  375. *
  376. * Reads a 32 bit integer from the address space of a given SDIO
  377. * function. If there is a problem reading the address,
  378. * 0xffffffff is returned and @err_ret will contain the error
  379. * code.
  380. */
  381. unsigned long sdio_readl(struct sdio_func *func, unsigned int addr,
  382. int *err_ret)
  383. {
  384. int ret;
  385. if (err_ret)
  386. *err_ret = 0;
  387. ret = sdio_memcpy_fromio(func, func->tmpbuf, addr, 4);
  388. if (ret) {
  389. if (err_ret)
  390. *err_ret = ret;
  391. return 0xFFFFFFFF;
  392. }
  393. return le32_to_cpu(*(u32*)func->tmpbuf);
  394. }
  395. EXPORT_SYMBOL_GPL(sdio_readl);
  396. /**
  397. * sdio_writel - write a 32 bit integer to a SDIO function
  398. * @func: SDIO function to access
  399. * @b: integer to write
  400. * @addr: address to write to
  401. * @err_ret: optional status value from transfer
  402. *
  403. * Writes a 32 bit integer to the address space of a given SDIO
  404. * function. @err_ret will contain the status of the actual
  405. * transfer.
  406. */
  407. void sdio_writel(struct sdio_func *func, unsigned long b, unsigned int addr,
  408. int *err_ret)
  409. {
  410. int ret;
  411. *(u32*)func->tmpbuf = cpu_to_le32(b);
  412. ret = sdio_memcpy_toio(func, addr, func->tmpbuf, 4);
  413. if (err_ret)
  414. *err_ret = ret;
  415. }
  416. EXPORT_SYMBOL_GPL(sdio_writel);
  417. /**
  418. * sdio_f0_readb - read a single byte from SDIO function 0
  419. * @func: an SDIO function of the card
  420. * @addr: address to read
  421. * @err_ret: optional status value from transfer
  422. *
  423. * Reads a single byte from the address space of SDIO function 0.
  424. * If there is a problem reading the address, 0xff is returned
  425. * and @err_ret will contain the error code.
  426. */
  427. unsigned char sdio_f0_readb(struct sdio_func *func, unsigned int addr,
  428. int *err_ret)
  429. {
  430. int ret;
  431. unsigned char val;
  432. BUG_ON(!func);
  433. if (err_ret)
  434. *err_ret = 0;
  435. ret = mmc_io_rw_direct(func->card, 0, 0, addr, 0, &val);
  436. if (ret) {
  437. if (err_ret)
  438. *err_ret = ret;
  439. return 0xFF;
  440. }
  441. return val;
  442. }
  443. EXPORT_SYMBOL_GPL(sdio_f0_readb);
  444. /**
  445. * sdio_f0_writeb - write a single byte to SDIO function 0
  446. * @func: an SDIO function of the card
  447. * @b: byte to write
  448. * @addr: address to write to
  449. * @err_ret: optional status value from transfer
  450. *
  451. * Writes a single byte to the address space of SDIO function 0.
  452. * @err_ret will contain the status of the actual transfer.
  453. *
  454. * Only writes to the vendor specific CCCR registers (0xF0 -
  455. * 0xFF) are permiited; @err_ret will be set to -EINVAL for *
  456. * writes outside this range.
  457. */
  458. void sdio_f0_writeb(struct sdio_func *func, unsigned char b, unsigned int addr,
  459. int *err_ret)
  460. {
  461. int ret;
  462. BUG_ON(!func);
  463. if (addr < 0xF0 || addr > 0xFF) {
  464. if (err_ret)
  465. *err_ret = -EINVAL;
  466. return;
  467. }
  468. ret = mmc_io_rw_direct(func->card, 1, 0, addr, b, NULL);
  469. if (err_ret)
  470. *err_ret = ret;
  471. }
  472. EXPORT_SYMBOL_GPL(sdio_f0_writeb);